【问题标题】:mongodb - unwinding nested subdocumentsmongodb - 展开嵌套子文档
【发布时间】:2019-01-19 16:25:55
【问题描述】:

对于以下数据集示例:

列表

{ _id: 1, included_lists: [ 2 ], items: [ "i1" ]}
{ _id: 2, included_lists: [], items: [ "i2", "i3" ]}

项目

{ _id: "i1", details: [{}, {}, {}]}
{ _id: "i2", details: [{}, {}, {}]}
{ _id: "i3", details: [{}, {}, {}]}

我想获取列表中的所有项目,包括附加到included_lists 的项目

例如:如果我们正在查看列表_id 1,我们应该得到项目i1, i2, i3

我知道如何做到这一点,其中涉及使用populate 或$lookup,但我不确定如何展开included_lists 内的嵌套items 并使用items 加入它们在原始列表中。

最后,我想要一个可以使用limit、skip 和match 的数据集。

我使用的是 mongoose,但原版 mongodb 代码也可以。

更新

我目前的想法是在一个查询中首先检索所有列表 ID,即

List.find({ _id: id}, { included_lists: 1})

然后,使用列表 ID,制作一个数组,即

var all_ids = [id, ...included_lists]

然后找到项目并放松

伪代码:

List
    .aggregate([
        {
            $match: {
                _id: {
                    $in: all_ids
                }
            }
        },
        { $lookup: {} }
        {
            $unwind: "$items"
        },
        {
            $project: {
                "list.name": 1,
                "list._id": 1,
                "items": 1
            }
        }
    ])

但我不想进行第一次查询来检索所有 list_id,我应该能够仅通过一个 _id 检索所有相关项目,然后通过 _id 检索其余项目included_lists

【问题讨论】:

  • 对于_id 1 只有一项items: [ "i1" ]?
  • @AnthonyWinzlet 这只是一个例子。不要认为有 1 个项目或 10,000 个项目真的很重要。这只是表明它是一个数组,因此是 X 数量的项目。

标签: javascript mongodb mongoose aggregation-framework


【解决方案1】:

您可以从 mongodb 3.6 及更高版本尝试以下聚合

List.aggregate([
  { "$match": { "_id": id }},
  { "$lookup": {
    "from": Items.collection.name,
    "let": { "items": "$items" },
    "pipeline": [
      { "$match": { "$expr": { "$in": [ "$_id", "$$items" ] } } }
    ],
    "as": "items"
  }},
  { "$lookup": {
    "from": Lists.collection.name,
    "let": { "included_lists": "$included_lists", "items": "$items" },
    "pipeline": [
      { "$match": { "$expr": { "$in": [ "$_id", "$$included_lists" ] } } },
      { "$lookup": {
        "from": Items.collection.name,
        "let": { "items": "$items" },
        "pipeline": [
          { "$match": { "$expr": { "$in": [ "$_id", "$$items" ] } } }
        ],
        "as": "items"
      }},
      { "$project": { "allItems": { "$concatArrays": [ "$$items", "$items" ]}}}
    ],
    "as": "included_lists"
  }},
  { "$unwind": "$included_lists" },
  { "$replaceRoot": { "newRoot": "$included_lists" }}
])

【讨论】:

  • 这是否包括在included_lists 中查找项目?
  • 然后我想将两个项目列表合并为一个,以便执行skip 和limit 功能。在那之后我应该使用mergeObject吗?
  • 酷!!!你可以使用$concatArrays 我已经更新了答案...你想要items 作为根文档吗?因为现在它们嵌套在included_lists
  • 是否可以同时包含他们的原始列表_id 和列表name?所以就像用$project : { name: 1, item: 1 }做一个$unwind
  • 我稍后会澄清这个答案
【解决方案2】:

您可以在 3.4 中尝试以下聚合。

初始$lookup 获取included_lists 的项目值,然后$concatArrays 合并查找的项目和项目。

第二个$lookup 获取项目详细信息,然后$unwind 以展平结果。

List.aggregate([
{"$lookup":{
  "from":name of the list collection,
  "localField":"included_lists",
  "foreignField":"_id",
  "as":"included_items"
}},
{"$unwind":"$included_items"},
{"$project":{"allItems":{"$concatArrays":["$items","$included_items.items"]}}},
{"$lookup":{
  "from":name of the item collection,
  "localField":"allItems",
  "foreignField":"_id",
  "as":"lookedup_items"
}},
{"$unwind":"$lookedup_items"},
{"$skip": some number},
{"$limit": some number}
])

【讨论】:

  • 这个不行,会报错,即引用字段名
  • 道歉。语法错误。现已修复和测试。请调整答案以包括集合名称以及跳过和限制值。
猜你喜欢
  • 1970-01-01
  • 2017-03-03
  • 2017-07-25
  • 1970-01-01
  • 2021-05-01
  • 2017-04-02
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多