在 C# 中使用 BsonRepresentation(BsonType.ObjectId) 与 BsonId 与 Ob

时间:2023-01-05
本文介绍了在 C# 中使用 BsonRepresentation(BsonType.ObjectId) 与 BsonId 与 ObjectId 装饰属性的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我是 mongodb 的新手,我喜欢不用担心架构内容是多么容易,我有一个问题,假设您想要 mongo 中的 Id 属性,而 mongo 使用 ObjectId 来表示属性 Id,所以到目前为止,我认为您可以拥有或装饰一个 Id,如下所示,

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?

推荐答案

1) 如果在强类型 TDocument 中有名为 Id、id 或 _id 的列类(集合中的项目类型),然后在 Mongo 中将生成名为 "_id" 的列.它还将为该列创建索引.如果尝试使用已存在的键插入项目,则会收到 重复键错误 异常.

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;放;} 将使用 ObjectId 的类型生成器,它看起来像 _id: ObjectId("57ade20771e59f422cc652d9").

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

public Guid _id { get;放;} 将使用 Guid 生成器生成类似 "_id" : BinData(3,"s2Td7qdghkywlfMSWMPzaA==").

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

public int Id { get;放;}, public string id { get;放;}, public byte[] _id { get;放;} 如果未指定,也将使用每种类型的默认值作为索引列.

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] 使您可以灵活地以任何您想要的方式命名该索引.[BsonId] public Guid SmthElseOtherThanId { get;放;}[BsonId] 公共字符串 StringId { get;放;} 将是索引;public Guid SmthElseOtherThanId { get;放;} 公共字符串 StringId { get;放;} 不会.mongodb 仍将在内部使用 _id.

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.

同理,public ObjectId SmthElseOtherThanId {get;set;} 没有 [BsonId] 修饰不会成为索引列.

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

3) [BsonRepresentation] 让您可以在 Mongo 类型与内部 .Net 类型之间进行调整,如果它们之间存在转换.

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

[BsonId] [BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get;放;} 等同于 public ObjectId Id { get;放;}.

[BsonId] [BsonRepresentation(BsonType.ObjectId)] 公共字符串 Id { get;放;} 是不同的.Mongo 会自己自动生成对象 id,但是你可以在 .net 中使用字符串,过滤查询等,因为对象 id 和字符串之间存在转换.

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.

[BsonId] [BsonRepresentation(BsonType.ObjectId)] public byte[] Id { get;放;}[BsonId] [BsonRepresentation(BsonType.ObjectId)] public int Id { get;放;} 将失败,ObjectId 不是 ByteArraySerializer/Int32Serializer 消息的有效表示.

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.

但是 [BsonId] [BsonRepresentation(BsonType.String)] public int StringId { get;放;} 就好了.

这篇关于在 C# 中使用 BsonRepresentation(BsonType.ObjectId) 与 BsonId 与 ObjectId 装饰属性的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Find 和 FindAsync 之间的区别 下一篇:手动为 MongoDB 查询提供参数以支持整理功能(对于不区分大小写的索引)

相关文章

最新文章