Use ObjectId.GenerateNewId() or leave MongoDB to create one? Use ObjectId.GenerateNewId() or leave MongoDB to create one? mongodb mongodb

Use ObjectId.GenerateNewId() or leave MongoDB to create one?


When you insert a new mongodb document the son driver check if exist a property with the BsonId AttributeClass. If exist and is null it create a new ObjectId, if doesn't exist during the document insertion mongodb will generate e new ObjectId.
Sometimes users encounter problem with "only zero" ObjectId, for this reason my suggestion is to use a combination of BsonID attribute class and ObjectId.GenerateNewId so you are sure that that property will not have weird behaviour.
e.g.

public class SomeClass {    [BsonId]    public ObjectId MySuperId { get; set; }    public SomeClass() {        this.MySuperId = ObjectId.GenerateNewId();    }}


if you want to create nested object in Mongo then you will need to generate ObjectId otherwise it will be null