mongodb-展开嵌套的子文档 [英] mongodb - unwinding nested subdocuments

查看:499
本文介绍了mongodb-展开嵌套的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下数据集示例:

列表

{ _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

I want to grab all the items for a list, including the ones attached to the included_lists

例如:如果我们查看列表_id 1,则应获取项目i1, i2, i3

For example: if we're looking at list _id 1, we should get items i1, i2, i3

我有一个方法,涉及使用populate$lookup,但是我不确定如何展开included_lists中嵌套的items并将它们与items结合在一起在原始列表中.

I have an idea how to do this, which involves using populate or $lookup, but I'm not sure how to unwind the nested items inside the included_lists and join them with the items in the original list.

最后,我希望有一个可以使用limitskipmatch的数据集.

In the end, I would like to have a dataset where I am able to use limit, skip and match.

我正在使用猫鼬,但是香草mongodb代码也可以.

I'm using mongoose, but vanilla mongodb code would also be fine.

更新

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

My current idea of how to do this is to retrieve all of the list ids first in one query i.e.

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

然后使用列表ID对该数组进行数组,即

Then, with the list ids, make an array of that i.e.

var all_ids = [id, ...included_lists]

然后只需找到物品并放松

Then just find the items and unwind

伪代码:

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

But I don't want to have to do a first query to retrieve all the list_ids, I should be able to retrieve all related items just through one _id which would then be able to retrieve the rest of the items through included_lists

推荐答案

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

You can try below aggregation in 3.4.

初始 $lookup 以获得项目值include_lists后跟 $concatArrays 以合并查找的项目,并项目.

Initial $lookup to get the items values for included_lists followed by $concatArrays to merge the looked up items and items.

第二个 $lookup ,以获取随后的商品详细信息通过 $unwind 进行拼合.

Second $lookup to get the item details followed by $unwind to flatten the results.

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}
])

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

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