I am using the .Net driver - mongodb C # for monngodb. When pasting a document, I want to copy the generated field [BsonId] ObjectId Idto another field in the document (duplicating the value)
[BsonId] ObjectId Id
I can run Insert, which will generate the key, and then run the update to copy the key to another field, but this will leave db in a “soft” state for a (short) period of time.
Is it possible to do this "atomically" or am I using mongodb incorrectly?
You cannot do this if you want the update to be atomic.
Id, ObjectId.GenerateNewId, MongoDB:
ObjectId.GenerateNewId
entity.Id = ObjectId.GenerateNewId(); entity.IdCopy = entity.Id;