【问题标题】:Issue retrieving subdocuments from MongoDB从 MongoDB 检索子文档的问题
【发布时间】:2017-07-13 14:48:27
【问题描述】:

我有以下数据集:

{
    "_id" : ObjectId("59668a22734d1d48cf34de08"),
    "name" : "Nobody Cares",
    "menus" : [ 
        {
            "_id" : "menu_123",
            "name" : "Weekend Menu",
            "description" : "A menu for the weekend",
            "groups" : [ 
                {
                    "name" : "Spirits",
                    "has_mixers" : true,
                    "sizes" : [ 
                        "Single", 
                        "Double"
                    ],
                    "categories" : [ 
                        {
                            "name" : "Vodka",
                            "description" : "Maybe not necessary?",
                            "drinks" : [ 
                                {
                                    "_id" : "drink_123",
                                    "name" : "Absolut",
                                    "description" : "Fancy ass vodka",
                                    "sizes" : [ 
                                        {
                                            "_id" : "size_123",
                                            "size" : "Single",
                                            "price" : 300
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ],
            "mixers" : [ 
                {
                    "_id" : "mixer_1",
                    "name" : "Coca Cola",
                    "price" : 150
                }, 
                {
                    "_id" : "mixer_2",
                    "name" : "Lemonade",
                    "price" : 120
                }
            ]
        }
    ]
}

我正在尝试从该数据集中检索单一饮料,我正在使用以下聚合查询:

db.getCollection('places').aggregate([
    { $match : {"menus.groups.categories.drinks._id" : "drink_123"} },
    { $unwind: "$menus" },
    { $project: { "_id": 1, "menus": { "groups": { "categories": { "drinks": { "name": 1 } } } }  } }
])

但是,它会返回数据集的完整结构以及正确的数据。

所以代替:

{
  "_id": "drink_123",
  "name": "Absolut"
}

我明白了:

{
  "_id": ObjectId("59668a22734d1d48cf34de08"),
  "menus": {
    "groups": {
      "categories": {
        "drinks": { "name": "Absolut" } 
      }
    }
  }
}

例如。任何想法如何只检索子文档?

【问题讨论】:

  • 是的。 扁平化数组。您在这里尝试做的事情并没有给您带来您认为的优势。事实上,它使查询和更新变得更糟。因此,您的“嵌套”实际上在这里更好地反映为单个元素数组的“属性”。这很容易更新和查询,而不是这里真正不可行的形式。您似乎误认为“组”和“类别”以这种格式对您有用。它不是。然而,作为“属性”“它会很有用”但目前这对性能或原子更新不利。

标签: mongodb mongodb-query


【解决方案1】:

如果您需要保留深度嵌套的模型,那么此调用将产生所需的输出:

db.getCollection('places').aggregate([
    { $match : {"menus.groups.categories.drinks._id" : "drink_123"} },
    { $project: {"_id": '$menus.groups.categories.drinks._id', name: '$menus.groups.categories.drinks.name'}},
    { $unwind: "$name" },
    { $unwind: "$name" },
    { $unwind: "$name" },
    { $unwind: "$name" },
    { $unwind: "$_id" },
    { $unwind: "$_id" },
    { $unwind: "$_id" },
    { $unwind: "$_id" }
])

大量展开是drinks 子文档深度嵌套的结果。

不过,FWIW,这种查询可能确实表明该模型不是“阅读友好”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 2015-05-25
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多