Reputation: 563
Based on the documentation, go.mongodb.org/mongo-driver
doesn't seem to provide a way to auto-increment ID when it is upserting a document that has not provided an ID.
type Document struct {
ID int `bson:"_id"`
Foo string `bson:"foo"`
}
document := &Document{Foo: "test"}
filter := bson.M{"_id": bson.M{"$eq": document.ID}}
update := bson.M{"$set": document}
res, err := mongoClient.Database(dbName).
Collection(collectionName).
UpdateOne(ctx, filter, update,
options.Update().SetUpsert(true))
In the code example above, ID will be defaulted to the zero value of int, which is 0
, and will be persisted in MongoDB as {"_id":0,"foo":"test"}
.
Is there a clean way for auto-incrementing ID when ID is not provided using mongo-driver
, without doing the logic of tracking the last ID myself? Say for example there are already 5 documents in the DB, then running the code above will persist {"_id":6,"foo":"test"}
when ID is not provided.
Upvotes: 3
Views: 5623
Reputation: 31
You can also do the following:
type Document struct {
ID int `bson:"_id,omitempty"`
Foo string `bson:"foo"`
}
Upvotes: 3
Reputation: 323
I found the same problem, the solution that i thought was to define the Document struct without ID:
type Document struct {
Foo string `bson:"foo"`
}
Then, if an InsertOne operation is executed using the mongo-driver:
res, err := mongoClient.Database(dbName).Collection(collectionName).InsertOne(ctx, document)
The _id will be created automatically in the database(You can repeat the InsertOne operation several times and new _ids will appear).
The mongodb documentation explains this behaviour: "If the document does not specify an _id field, then mongod will add the _id field and assign a unique ObjectId for the document before inserting." (You can read more details in https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/#db.collection.insertOne)
In case that you need the new created _id for some reason, you can use the following snippet as a reference to retrieve it:
fmt.Println("New Document created with mongodb _id: " + res.InsertedID.(primitive.ObjectID).Hex())
(primitive.ObjectID is related with this: import "go.mongodb.org/mongo-driver/bson/primitive")
Hope this helps!
Upvotes: 3