MongoDB - DBRef
我在使用 DBRef 时遇到了一些麻烦,看看这种情况:
db.fruit.save ({"_id" : "1" , "name" : "apple"});
db.fruit.save ({"_id" : "2" , "name" : "grape"});
db.fruit.save ({"_id" : "3" , "name" : "orange"});
db.fruit.save ({"_id" : "4" , "name" : "pineapple"});
db.basket.save ({"_id" : "1", "items" : [
{"$ref" : "fruit", "$id" : "1", "quantity" : 5},
{"$ref" : "fruit", "$id" : "3", "quantity" : 10}
]})
现在,让我们找到“篮子”集合:
> db.basket.find ()
{ "_id" : "1", "items" : [
{
"$ref" : "fruit",
"$id" : "1"
},
{
"$ref" : "fruit",
"$id" : "3"
}
] }
“数量”属性消失了?!有人知道为什么吗?还有其他选择吗?
谢谢。
I am having some troubles with DBRef, look this case:
db.fruit.save ({"_id" : "1" , "name" : "apple"});
db.fruit.save ({"_id" : "2" , "name" : "grape"});
db.fruit.save ({"_id" : "3" , "name" : "orange"});
db.fruit.save ({"_id" : "4" , "name" : "pineapple"});
db.basket.save ({"_id" : "1", "items" : [
{"$ref" : "fruit", "$id" : "1", "quantity" : 5},
{"$ref" : "fruit", "$id" : "3", "quantity" : 10}
]})
Now, lets find the "basket" collection:
> db.basket.find ()
{ "_id" : "1", "items" : [
{
"$ref" : "fruit",
"$id" : "1"
},
{
"$ref" : "fruit",
"$id" : "3"
}
] }
The "quantity" attribute disappeared ?! Anybody knows why ? Is there an alternative ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dbref 的语法是
但是您在 dbref 中添加了不支持的数量字段。这就是问题所在。把它放在外面,
看起来很可怕(可怕)
但我的建议是,完全放弃 dbref 并只使用像这样的简单结构,
这更干净,看起来像
Syntax for the dbref is
But you have added non-supported quantity field inside dbref. Thats the problem. take that outside
which kind of looks (scary)
But my advice is, ditch the dbref altogether and just use the simple structure like this
this is much cleaner, which will look like