MongoDB:自动生成的 ID 为零

发布于 2024-10-10 03:48:25 字数 1172 浏览 0 评论 0原文

我正在使用 MongoDB 和官方 C# 驱动程序 0.9

我只是检查嵌入简单文档的工作原理。

有 2 个简单的类:

public class User
{
    public ObjectId _id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Address> Addresses { get;set; }
}

public class Address
{
    public ObjectId _id { get; set; }
    public string Street { get; set; }
    public string House { get; set; }
}

我创建一个新用户:

var user = new User
{
    Name = "Sam",
    Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET" } })
};

collection.Insert(user.ToBsonDocument());

用户已成功保存,他的地址也是如此。

在 MongoDB shell 中输入后

db.users.find()

,我得到以下结果:

{ "_id" : ObjectId("4e572f2a3a6c471d3868b81d"), "Name" : "Sam",  "Addresses" : [
        {
                "_id" : ObjectId("000000000000000000000000"),
                "Street" : "BIGSTREET",
                "House" : "BIGHOUSE"
        }
] }

为什么地址'对象 id 为 0?

不过,使用地址进行查询是有效的:

collection.FindOne(Query.EQ("Addresses.Street", streetName));

它返回用户“Sam”。

I'm using MongoDB and official C# driver 0.9

I'm just checking how embedding simple documents works.

There are 2 easy classes:

public class User
{
    public ObjectId _id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Address> Addresses { get;set; }
}

public class Address
{
    public ObjectId _id { get; set; }
    public string Street { get; set; }
    public string House { get; set; }
}

I create a new user:

var user = new User
{
    Name = "Sam",
    Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET" } })
};

collection.Insert(user.ToBsonDocument());

The user is successfully saved, so is his address.

After typing

db.users.find()

in MongoDB shell, I got the following result:

{ "_id" : ObjectId("4e572f2a3a6c471d3868b81d"), "Name" : "Sam",  "Addresses" : [
        {
                "_id" : ObjectId("000000000000000000000000"),
                "Street" : "BIGSTREET",
                "House" : "BIGHOUSE"
        }
] }

Why is address' object id 0?

Doing queries with the address works though:

collection.FindOne(Query.EQ("Addresses.Street", streetName));

It returns the user "Sam".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半葬歌 2024-10-17 03:48:25

与其说这是一个错误,不如说这是一个未满足期望的情况。只有顶层 _id 会自动分配一个值。任何嵌入的 _ids 都应由客户端代码分配值(使用 ObjectId.GenerateNewId)。也有可能您甚至不需要 Address 类中的 ObjectId(它的目的是什么?)。

It's not so much a bug as a case of unmet expectations. Only the top level _id is automatically assigned a value. Any embedded _ids should be assigned values by the client code (use ObjectId.GenerateNewId). It's also possible that you don't even need an ObjectId in the Address class (what is the purpose of it?).

泡沫很甜 2024-10-17 03:48:25

使用 BsonId 属性:

public class Address
{
    [BsonId]
    public string _id { get; set; }
    public string Street { get; set; }
    public string House { get; set; }
}

识别 Id 字段或属性

识别哪个字段或属性
类是你可以写的Id:

公共类 MyClass {
    [BsonId]
    公共字符串 SomeProperty { 获取;放; }
}

驱动程序教程

编辑

它实际上不起作用。我稍后会检查原因。
如果您需要让它工作,请使用以下命令:

    [Test]
   public void Test()
    {
        var collection = Read.Database.GetCollection("test");

        var user = new User
        {
            Name = "Sam",
            Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET", _id = ObjectId.GenerateNewId().ToString() } })
        };

        collection.Insert(user.ToBsonDocument());
    }

Use BsonId attribute:

public class Address
{
    [BsonId]
    public string _id { get; set; }
    public string Street { get; set; }
    public string House { get; set; }
}

Identifying the Id field or property

To identify which field or property of
a class is the Id you can write:

public class MyClass {
    [BsonId]
    public string SomeProperty { get; set; }
}

Driver Tutorial

Edit

It's actually not working. I will check later why.
If you need get it work use following:

    [Test]
   public void Test()
    {
        var collection = Read.Database.GetCollection("test");

        var user = new User
        {
            Name = "Sam",
            Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET", _id = ObjectId.GenerateNewId().ToString() } })
        };

        collection.Insert(user.ToBsonDocument());
    }
水水月牙 2024-10-17 03:48:25

获取用户类型的集合:

var collection = db.GetCollection<User>("users");

如下初始化字段 _id:

var user = new User
{
   _id = ObjectId.Empty,
   Name = "Sam",
   Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET" } })
};

然后插入对象:

collection.InsertOne(user);

_id 字段将自动生成。

在此链接中,您将找到自定义的替代方法自动生成的 ID。

Get the collection as User type:

var collection = db.GetCollection<User>("users");

Initialize the field _id as follows:

var user = new User
{
   _id = ObjectId.Empty,
   Name = "Sam",
   Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET" } })
};

Then you insert the object:

collection.InsertOne(user);

The _id field will automatically be generated.

In this link you will find alternative ways to have customized auto-generated ID(s).

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