如何在将 POCO 与 mongodb C# 驱动程序一起使用时管理 _id 字段

发布于 2024-11-08 16:23:16 字数 280 浏览 2 评论 0原文

如果我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

苏璃陌 2024-11-15 16:23:16

当您插入一个对象时,如果它没有 _id 字段,则驱动程序会添加一个并将其设置为 12 字节 MongoDB ObjectId 值。

您只需向 POCO 添加一个 Id 属性,该属性将从 _id 反序列化:

public class Thingy
{
     public ObjectId Id { get; set; }
}

或者,如果您想委托另一个属性来映射到 _id 然后你可以用 BsonIdAttribute 修饰它,如下所示:

[BsonId]
public ObjectId MyKey { get; set; }   

_id 字段不必是 MongoDB ObjectId ,您可以将其设置为任何数据类型的任何值(数组除外),它只需要在集合中是唯一的。

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:

public class Thingy
{
     public ObjectId Id { get; set; }
}

Or, if you'd like to delegate another property to map onto _id then you can decorate it with the BsonIdAttribute, like this:

[BsonId]
public ObjectId MyKey { get; set; }   

The _id field doesn't have to be an MongoDB ObjectId, you can set it to any value of any data type (except an array), it just needs to be unique within the collection.

海之角 2024-11-15 16:23:16

您必须为 id 添加属性(或字段),并告诉序列化程序您要使用哪个 id 生成器。

[BsonId(IdGenerator = typeof(ObjectIdGenerator))]
public object ThingyId { get; set; }

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.

[BsonId(IdGenerator = typeof(ObjectIdGenerator))]
public object ThingyId { get; set; }

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

同展鸳鸯锦 2024-11-15 16:23:16

我通常包装 Thingy:

public class MongoThingy
{
    public ObjectId Id { get; set; }
    public Thingy Thingy { get; set; }
}

它使它变得更容易,因为类 Thingy 通常来自我无法控制的不同库。反序列化以便将其交给其他人进行处理也更容易。

I generally wrap Thingy:

public class MongoThingy
{
    public ObjectId Id { get; set; }
    public Thingy Thingy { get; set; }
}

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.

土豪我们做朋友吧 2024-11-15 16:23:16

添加属性如下:

public BsonObjectId Id { get; set; }

MongoDB 驱动在序列化\反序列化过程中自动将 Id 转换为 _id

Add a property as follows:

public BsonObjectId Id { get; set; }

The MongoDB driver automatically converts Id to _id during serialization\deserializtion.

撩起发的微风 2024-11-15 16:23:16
public class Thingy
{
      public ObjectId Id { get; set; }
      public string Foo { get; set; }
}

根据类

的需要,使用以下代码:

var collection = database.GetCollection<Thingy>("db_Thingy");
Thingy tg= new Thingy();
tg.Foo = "Hello";
collection.insert(tg);
public class Thingy
{
      public ObjectId Id { get; set; }
      public string Foo { get; set; }
}

According to class

Where needed, use the following code:

var collection = database.GetCollection<Thingy>("db_Thingy");
Thingy tg= new Thingy();
tg.Foo = "Hello";
collection.insert(tg);
箜明 2024-11-15 16:23:16

方式 https://learn.microsoft.com/pt-br/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-5.0&tabs=visual-studio

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace BooksApi.Models
{
    public class Book
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }

        [BsonElement("Name")]
        public string BookName { get; set; }

        (...)
    }
}

The way by https://learn.microsoft.com/pt-br/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-5.0&tabs=visual-studio

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace BooksApi.Models
{
    public class Book
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }

        [BsonElement("Name")]
        public string BookName { get; set; }

        (...)
    }
}
苏佲洛 2024-11-15 16:23:16

正如 Chris Christopher 在上面的评论中建议的,您可以使用 BsonIgnoreExtraElements 类属性:

[BsonIgnoreExtraElements]
public class Thingy
{
     public string Foo { get; set; }
}

这样您就不必添加带有其他 BSon 属性的 Id 属性。

As suggested by Chris Christopher in a comment above, you can use BsonIgnoreExtraElements class attribute:

[BsonIgnoreExtraElements]
public class Thingy
{
     public string Foo { get; set; }
}

This way you don't have to add an Id property with some other BSon attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文