MongoDB - DBRef [英] MongoDB - DBRef

查看:192
本文介绍了MongoDB - 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:菠萝});
$ b $ db.basket.save({_id:1,items:[
{$ ref:fruit,$ id:1 ,数量:
{$ ref:fruit,$ id:3,quantity:10}
]})

现在,让我们找到篮子集合:

 > db.basket.find()
{_id:1,items:[
{
$ ref:fruit,
$ id :1
},
{
$ ref:fruit,
$ id:3
}
]}

数量属性消失了!有人知道为什么吗?有没有其他的选择?



谢谢。

dbref

<$ p $ {$ ref:< collname>,$ id:< idvalue> [,$ db:< dbname>]}

但是您在dbref中添加了不受支持的数量字段。那就是问题所在。外面拿着那个

  db.basket.save({_id:1,items:[
{quantity:5,item:{$ ref:fruit,$ id:1}},
{quantity:10,item:{$ ref fruit,$ id:3}}
]})

(可怕)

  {
_id:1,
:[

quantity:5,
item:{
$ ref:fruit,
$ id: 1

},
{
quantity:10,
item:{
$ ref:fruit,
$ id:3
}
}
]
}

但是我的建议是,把dbref完全抛在一边,用这个简单的结构

  db.basket.save({_id:1,items:[
{item_id:1,quantity:50},
{item_id:3,quantity:10}
]})

清洁剂,看起来像

  {
_id:1,
items :[

item_id:1,
quantity:50
},
{
item_id:3 ,
quantity:10
}
]
}


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.

解决方案

Syntax for the dbref is

  { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }

But you have added non-supported quantity field inside dbref. Thats the problem. take that outside

db.basket.save ({"_id" : "1", "items" : [
    {"quantity" : 5 , item : {"$ref" : "fruit", "$id" : "1"}},
    {"quantity" : 10, item : {"$ref" : "fruit", "$id" : "3"}}
]})

which kind of looks (scary)

{
    "_id" : "1",
    "items" : [
        {
            "quantity" : 5,
            "item" : {
                "$ref" : "fruit",
                "$id" : "1"
            }
        },
        {
            "quantity" : 10,
            "item" : {
                "$ref" : "fruit",
                "$id" : "3"
            }
        }
    ]
}

But my advice is, ditch the dbref altogether and just use the simple structure like this

db.basket.save ({"_id" : "1",items:[
                        {item_id:"1",quantity:50},
                        {item_id:"3",quantity:10}
                ]})

this is much cleaner, which will look like

{
    "_id" : "1",
    "items" : [
        {
            "item_id" : "1",
            "quantity" : 50
        },
        {
            "item_id" : "3",
            "quantity" : 10
        }
    ]
}

这篇关于MongoDB - DBRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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