如何在将 POCO 与 mongodb C# 驱动程序一起使用时管理 _id 字段
如果我想用 POCO 读取和写入 mongo 数据,
public class Thingy
{
public string Foo {get;set;}
}
...
coll.Insert(new Thing(Foo = "hello"));
当我读回时,我会收到一条错误消息,指出 _id 是意外属性(确实如此)。然后我在类中添加了一个名为 _id 的字段。现在插入不起作用,说明 _id 字段不能为空。尝试过 BsonIgnoreIfNull 属性,但不起作用。
If I want to read and write mongo data with a POCO
public class Thingy
{
public string Foo {get;set;}
}
...
coll.Insert(new Thing(Foo = "hello"));
When I read back I get a failure saying that _id is an unexpected attribute (which it is). So then I added a field called _id to the class. Now the insert doesnt work saying that the _id field cannot be null. A tried BsonIgnoreIfNull attribute, that didnt work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当您插入一个对象时,如果它没有
_id
字段,则驱动程序会添加一个并将其设置为 12 字节 MongoDB ObjectId 值。您只需向 POCO 添加一个
Id
属性,该属性将从_id
反序列化:或者,如果您想委托另一个属性来映射到
_id
然后你可以用BsonIdAttribute
修饰它,如下所示:_id
字段不必是 MongoDBObjectId
,您可以将其设置为任何数据类型的任何值(数组除外),它只需要在集合中是唯一的。When you insert an object, if it doesn't have an
_id
field then the driver adds one and sets it to a 12-byte MongoDB ObjectId value.You just need to add an
Id
property to your POCO, which will be deserialised from_id
:Or, if you'd like to delegate another property to map onto
_id
then you can decorate it with theBsonIdAttribute
, like this:The
_id
field doesn't have to be an MongoDBObjectId
, you can set it to any value of any data type (except an array), it just needs to be unique within the collection.您必须为 id 添加属性(或字段),并告诉序列化程序您要使用哪个 id 生成器。
MongoDb 驱动程序中有 3 个可用,或者您可以编写自己的驱动程序。更多信息请访问 http://www.mongodb.org/显示/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-WriteacustomIdgenerator
You have to add a property (or field) for id and tell serializer which id generator you'd like to use.
There are 3 available in MongoDb Driver or you can write your own. More info at http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-WriteacustomIdgenerator
我通常包装 Thingy:
它使它变得更容易,因为类 Thingy 通常来自我无法控制的不同库。反序列化以便将其交给其他人进行处理也更容易。
I generally wrap Thingy:
It makes it a lot easier, as often times class Thingy comes from a different library over which I have no control. It's also easier to deserialize in order to hand it over to someone else for processsing.
添加属性如下:
MongoDB 驱动在序列化\反序列化过程中自动将
Id
转换为_id
。Add a property as follows:
The MongoDB driver automatically converts
Id
to_id
during serialization\deserializtion.根据类
的需要,使用以下代码:
According to class
Where needed, use the following code:
方式 https://learn.microsoft.com/pt-br/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-5.0&tabs=visual-studio
The way by https://learn.microsoft.com/pt-br/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-5.0&tabs=visual-studio
正如 Chris Christopher 在上面的评论中建议的,您可以使用
BsonIgnoreExtraElements
类属性:这样您就不必添加带有其他
BSon
属性的 Id 属性。As suggested by Chris Christopher in a comment above, you can use
BsonIgnoreExtraElements
class attribute:This way you don't have to add an Id property with some other
BSon
attribute.