MongoDB:自动生成的 ID 为零
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与其说这是一个错误,不如说这是一个未满足期望的情况。只有顶层 _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?).
使用 BsonId 属性:
驱动程序教程
编辑
它实际上不起作用。我稍后会检查原因。
如果您需要让它工作,请使用以下命令:
Use BsonId attribute:
Driver Tutorial
Edit
It's actually not working. I will check later why.
If you need get it work use following:
获取用户类型的集合:
如下初始化字段 _id:
然后插入对象:
_id 字段将自动生成。
在此链接中,您将找到自定义的替代方法自动生成的 ID。
Get the collection as User type:
Initialize the field _id as follows:
Then you insert the object:
The _id field will automatically be generated.
In this link you will find alternative ways to have customized auto-generated ID(s).