MongoDB的:自动生成的ID是零 [英] MongoDB: automatically generated IDs are zeroes

查看:936
本文介绍了MongoDB的:自动生成的ID是零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我只是检查如何嵌入简单的文档工作。



有两个简单的类:

 公共类用户
{
公众的ObjectId _id {得到;组; }
公共字符串名称{;组; }
公开的IEnumerable<地址>地址{获取;设置; }
}

公共类地址
{
公众的ObjectId _id {搞定;组; }
公共字符串街{搞定;组; }
公共字符串府{搞定;组; }
}



我创建一个新用户:

  VAR用户=新用户
{
NAME =山姆,
类地址=(新地址[] {新地址{众议院=BIGHOUSE,街道=BIGSTREET}})
};

collection.Insert(user.ToBsonDocument());



用户保存成功,所以他的地址。



后键入

  db.users.find()

在MongoDB的外壳,我得到了以下结果:

  {_id:的ObjectId(4e572f2a3a6c471d3868b81d),名:山姆,地址:
{
_id:的ObjectId(000000000000000000000000),
街:BIGSTREET,
豪斯医生:BIGHOUSE
}
]}

为什么地址的对象ID 0



与地址做查询工作虽然:?

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



它返回用户山姆。


解决方案

使用BsonId属性:

 公共类地址
{
[BsonId]
公共字符串_id {搞定;组; }
公共字符串街{搞定;组; }
公共字符串府{搞定;组; }
}




识别标识字段或财产



要确定哪些字段或
A类的属性是ID,你可以这样写:

 公共类MyClass的{
[BsonId]
公共字符串SomeProperty {搞定;组; }
}




Driver教程



修改



它实际上是行不通的。我将在后面为什么检查。
。如果你需要得到它的工作使用下列内容:

  [测试] 
公共无效测试()
{
变种集合= Read.Database.GetCollection(测试);

VAR用户=新用户
{
NAME =山姆,
类地址=(新地址[] {新地址{楼=BIGHOUSE,街=BIGSTREET,_id = ObjectId.GenerateNewId()的ToString()}})
}。

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


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".

解决方案

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());
    }

这篇关于MongoDB的:自动生成的ID是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆