Id Member
Unique Identifier
Each top level document in MongoDB contains an _id field that uniquely identifies documents in the collection. This field can be mapped from/to a public property in C# models. By convention public members named Id, id and id will be used as the identifier. MongoDB will ensure to generate a value for your identifier field when inserting documents and deserialize it back your model member during reads.
The identifier member can be of any type but this doesn't mean that MongoDB will be able to automatically generate the value during insertions
Supported MongoDB Id generators
You can use the following types for your Id identifier type:
String
Let's assume that you have a Message class with a string Id public member:
var message = new Message {Text = "hello world"};
await messagesCollection.InsertOneAsync(message);
Utils.Log(message.ToBsonDocument());public class Message
{
public string Id { get; set; }
public string Text { get; set; }
}// Id hasn't be recognized by MongoDB
// as an auto generated property
{
"_id": null,
"text": "hello world"
}When you want to use a string member as an auto generated MongoDB Id member, then just declaring it as a string isn't enough. You need to inform the driver which IdGenerator should use so that can understand if the Id member has been assigned a value or not.
In case you don't do this, the next time you try to insert a document you will get a E11000 duplicate key error collection error
The correct way to use a string Id member as identifier field is the following:
In case you want to name your identifier member other than Id, id or _id, make sure you use the [BsonId]attribute on it and MongoDB driver will do the rest
Guid
Guid is one of the most used types for identifying documents and MongoDB fully support them. In fact, in case you use it along with a convention compatible Id member, you don't even need to specify the IdGenerator.
COMB Guid
MongoDB supports generating Guid values using the COMB algorithm and you can use it as follow:
ObjectId
MongoDB loves ObjectId β₯οΈ which is a 12-byte special type for MongoDB which is fast to generate and contains:
A timestamp value representing the ObjectId's creations (4-byte)
An auto-incrementing counter (3-byte)
A random value (5-byte)
Convention based names with ObjectId type don't require the [BsonId] attribute.
If you want to use a custom identifier name, just add the [BsonId] attribute. Defining the ObjectIdGenerator is optional.
BsonObjectId
Everything applies for the ObjectId type applies for BsonObjectId as well.
If you want to use a custom identifier name, just add the [BsonId] attribute. Defining the BsonObjectIdGenerator is optional.
NullIdChecker Generator
In case you want to ensure that the identifier field has been assigned a value before sending the query to MongoDB, you can use the NullIdChecker generator.
Trying to insert a document without assigning a value first for an identifier field with NullIdChecker generator will throw System.InvalidOperationException: <field> cannot be null.
Last updated