Let's say you have a simple struct
type User struct {
ID uint64 `json:"id" bson:"_id"`
UserName string `json:"user_name" bson:"userName"`
Email string `json:"email" bson:"email"`
}
u := User{
userName: "me",
Email: "me@mail.com"
}
I am trying to insert this object in MongoDB collection like so:
r, err := collection.InsertOne(context.TODO(), u)
The ID field here has a value of 0 because I didn't specify a value.
The problem is that MongoDB does not auto-generate the _id field but instead, sets the value to equal 0, which is very logical but not the outcome I would like.
Is there a way to auto-generate the _id with this method?