skip to content
Alvin Lucillo

Id not set on doc creation

/ 1 min read

In Go, if you create a document whose struct has bson:"_id" ID definition but the value isn’t set, it will contain a zero value (000000000000000000000000). MongoDB will not generate the value because there is already a value set to the ID. You can either:

  1. Assign value to ID, or
  2. Update the tag with bson:"_id,omitempty"

The latter will remove the id field during the encoding process, causing MongoDB to generate a unique value for it.