Difference between decorating a property in C # with BsonRepresentation (BsonType.ObjectId) vs BsonId vs ObjectId

I am new to mongodb and I like how easy it is to not worry about circuit things. I have a question that you want the Id property in mongo and mongo to use ObjectId to denote property identifiers, so far I see that you can have or decorate Id as follows:

 public ObjectId Id {get; set;} //or [BsonId] public string Id {get; set;} //or [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id {get; set;} 

Can someone explain to me why most people choose the latter type, and what happens, and how flexibility helps. Thanks

+5
source share
1 answer

1) If you have a column named Id, id or _id , in your strongly typed TDocument class (the type of the item in the collection), then a column called "_id" will be created in Mongo. It will also create an index for this column. You get a duplicate key error exception if you try to insert an item with an existing key.

public ObjectId Id { get; set; } public ObjectId Id { get; set; } will use a type generator for ObjectId , and it will look like _id: ObjectId("57ade20771e59f422cc652d9") .

public Guid _id { get; set; } public Guid _id { get; set; } will use the Guid generator to create smth, like "_id" : BinData(3,"s2Td7qdghkywlfMSWMPzaA==") .

public int Id { get; set; } public int Id { get; set; } , public string id { get; set; } public string id { get; set; } public string id { get; set; } , public byte[] _id { get; set; } public byte[] _id { get; set; } public byte[] _id { get; set; } will also be index columns, using default values ​​for each type if they are not specified.

2) [BsonId] gives you the flexibility to name this index in any way. [BsonId] public Guid SmthElseOtherThanId { get; set; } [BsonId] public Guid SmthElseOtherThanId { get; set; } and [BsonId] public string StringId { get; set; } [BsonId] public string StringId { get; set; } [BsonId] public string StringId { get; set; } will be indices; public Guid SmthElseOtherThanId { get; set; } public Guid SmthElseOtherThanId { get; set; } and public string StringId { get; set; } public string StringId { get; set; } public string StringId { get; set; } will not be. mongodb will still use _id internally.

The same logic, public ObjectId SmthElseOtherThanId {get; set;} public ObjectId SmthElseOtherThanId {get; set;} without decoration [BsonId] will not be an index column.

3) [BsonRepresentation] allows you to manipulate the Mongolian type and the internal .Net type, if there is a conversion between them.

The presence of [BsonId] [BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get; set; } identical to public ObjectId Id { get; set; } public ObjectId Id { get; set; } public ObjectId Id { get; set; } .

If [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } is different. Mongo automatically generates object identifiers, however you can use strings in .net, filters, etc., because there is a conversion between the object identifier and the string.

If [BsonId] [BsonRepresentation(BsonType.ObjectId)] public byte[] Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public byte[] Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public byte[] Id { get; set; } or [BsonId] [BsonRepresentation(BsonType.ObjectId)] public int Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public int Id { get; set; } [BsonId] [BsonRepresentation(BsonType.ObjectId)] public int Id { get; set; } will end with the message ObjectId not a valid representation for a ByteArraySerializer / Int32Serializer .

But [BsonId] [BsonRepresentation(BsonType.String)] public int StringId { get; set; } [BsonId] [BsonRepresentation(BsonType.String)] public int StringId { get; set; } [BsonId] [BsonRepresentation(BsonType.String)] public int StringId { get; set; } will be just fine.

+9
source

All Articles