skip to content
Alvin Lucillo

Auto-generated _id

/ 1 min read

💻 Tech

If you’re using the mongo-driver/mongo package, and you want to automatically create the _id value with InsertOne, you need to ensure that:

  1. Your struct has a property for _id with type of pointer to the ObjectID like so: ID *primitive.ObjectID `bson:"_id,omitempty"
  2. You don’t populate _id with a value.

If your _id is not using pointer semantics, you will have zeroes as the value.