mongodb-汇总和解散外国参考文件 [英] mongodb - aggregating and unwinding foreign ref documents

查看:74
本文介绍了mongodb-汇总和解散外国参考文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,对于我的示例数据库设置:

So for my example database set up:

db.lists.insertMany([
    { _id: "1", name: "list1", included_lists: ["2"], items: ["i1"] },
    { _id: "2", name: "list2", included_lists: [], items: ["i2", "i3"] }
])


db.items.insertMany([
    { _id: "i1", name: "item1", details: [{}, {}, {}] },
    { _id: "i2", name: "item2", details: [{}, {}, {}] },
    { _id: "i3", name: "item3", details: [{}, {}, {}] }
])

我目前通过以下方式获取商品数据:

I'm currently getting my items data via:

db.lists.aggregate([
    { "$match": { "_id": { "$in": ["1", "2"] } } },
    {
        "$lookup": {
            "from": "items",
            "localField": "items",
            "foreignField": "_id",
            "as": "item"
        }
    },
    { "$unwind": "$item" },
    {
        "$facet": {
            "results": [
                { "$skip": 0 },
                { "$limit": 10 },
                {
                    "$project": {
                        name: 1,
                        item: 1
                    }
                }
            ],
            "total": [
                { "$count": "total" },
            ]
        }
    }
]).pretty()

返回:

{
    "results" : [
        {
            "_id" : "1",
            "name" : "list1",
            "item" : {
                "_id" : "i1",
                "name" : "item1",
                "details" : [
                    {

                    },
                    {

                    },
                    {

                    }
                ]
            }
        },
        {
            "_id" : "2",
            "name" : "list2",
            "item" : {
                "_id" : "i2",
                "name" : "item2",
                "details" : [
                    {

                    },
                    {

                    },
                    {

                    }
                ]
            }
        },
        {
            "_id" : "2",
            "name" : "list2",
            "item" : {
                "_id" : "i3",
                "name" : "item3",
                "details" : [
                    {

                    },
                    {

                    },
                    {

                    }
                ]
            }
        }
    ],
    "total" : [
        {
            "total" : 3
        }
    ]
}

我想做的是删除{ "$match": { "_id": { "$in": ["1", "2"] } } },,因为我想删除获取ID数组所需的查询,而只是从列表_id及其included_lists id中获取所有ID .然后像我的结果一样返回所有items返回.

What I'm trying to do, is remove the { "$match": { "_id": { "$in": ["1", "2"] } } }, as I want to remove the query needed to get the array of ids, and instead just get all the ids from list _id and its included_lists ids. Then have return all the items return like my result.

此问题类似于: mongodb-展开嵌套的子文档,但由于模棱两可和缺少db文档,我感到很震惊.

This question is similar to: mongodb - unwinding nested subdocuments but I've reasked due to ambiguity and lack of db documents.

推荐答案

您可以通过图形查找然后分组

you can do it with graph lookup and then group

db.lists.aggregate([
    { "$match": { "_id": { "$in": ["1"] } } },
    {
      $graphLookup: {
      from: "lists",
      startWith: "$_id" ,
      connectFromField: "included_lists",
      connectToField: "_id",
      as: "connected",
   }
        },
  
    {$unwind:"$connected"},
    { $group:{_id:"$connected._id",items:{$first:'$connected.items'},name:{$first:'$connected.name'}}},
     {
        "$lookup": {
            "from": "items",
            "localField": "items",
            "foreignField": "_id",
            "as": "item"
        }
    },
    { "$unwind": "$item" },
    {
        "$facet": {
            "results": [
                { "$skip": 0 },
                { "$limit": 10 },
                {
                    "$project": {
                        name: 1,
                        item: 1
                    }
                }
            ],
            "total": [
                { "$count": "total" },
            ]
        }
    }
    
    

   
]).pretty()

这篇关于mongodb-汇总和解散外国参考文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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