Mongodb C # driver - copy id to another field on insert

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)

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?

+4
source share
1 answer

You cannot do this if you want the update to be atomic.

Id, ObjectId.GenerateNewId, MongoDB:

entity.Id = ObjectId.GenerateNewId();
entity.IdCopy = entity.Id;
+3

All Articles