Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

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

Am new to mongodb and am liking how easy it is not to worry about schema stuff, I have a question suppose you want an Id property in mongo and mongo uses ObjectId to denote property Id's, so far i see you can have or decorate an 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 anyone explain to me why most people choose the last type, and whats going on and how the flexibility helps. Thanks?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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

2) [BsonId] gives you the flexibility of naming that index any way you want. [BsonId] public Guid SmthElseOtherThanId { get; set; } and [BsonId] public string StringId { get; set; } will be indexes; public Guid SmthElseOtherThanId { get; set; } and public string StringId { get; set; } won't. mongodb will still use _id internally.

Same logic, public ObjectId SmthElseOtherThanId {get; set;} with no [BsonId] decoration won't be an index column.

3) [BsonRepresentation] lets you juggle with the Mongo type vs the internal .Net type, if there's a conversion between them.

Having [BsonId] [BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get; set; } is identical to public ObjectId Id { get; set; }.

Having [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } is different however. Mongo will auto generate object ids by himself, however you will be able to use strings in .net, filter queries etc., because there is a conversion between object id and string.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...