@Goshleg_N_A Is it possible you’re mixing Go Driver v1.x and v2.x packages? I was able to reproduce the error you’re seeing with this example:
package main
import (
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
func main() {
mongoClient := mongo.Connect()
type User struct {
ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
Username string `bson:"username"`
PasswordHash string `bson:"password_hash"`
}
usersCollection := mongoClient.Database("test").Collection("users")
ctx := context.TODO()
user := User{
Username: "anyuser",
PasswordHash: "somehash",
}
if _, err := usersCollection.InsertOne(ctx, user); err != nil {
panic(err)
}
var userResult User
err := usersCollection.FindOne(ctx, bson.D{}).Decode(&userResult)
if err != nil {
panic(err)
}
}
Notice that primitive.ObjectID comes from the non-v2 package. Try changing that type to bson.ObjectID and see if it resolves your problem.