Mongo db primitive Object id issue

I have a collection in mongodb called users.
I have created a struct for same:

type UserType struct {
	ID       primitive.ObjectID `bson:"_id"`
	Username string             `bson:"username"`
	Fname    string             `bson:"firstName"`
	Lname    string             `bson:"lastName"`

I am not setting ID as mongo set it by default if id is not passed while inserting document. Due to golang null safety ID is set to ‘00000000000000’. Mongo db provides a primitive.NewObjectId function to generate a id and pass it to struct.
So when i donot pass i get error of duplicate id which is obvious as document with id 00000000000000 already exists.
I am not sure if i should use it. Is there any another way around ?

ID  primitive.ObjectID `bson:"_id,omitempty"`

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.