MongoDB - DBRef

发布于 2024-12-24 20:41:59 字数 689 浏览 1 评论 0原文

我在使用 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 技术交流群。

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

发布评论

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

评论(1

怂人 2024-12-31 20:41:59

dbref 的语法是

  { $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",
    "items" : [
        {
            "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
        }
    ]
}

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