mongodb-聚集和展开外部参考文件

时间:2018-08-13 06:54:31

标签: javascript mongodb mongoose

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

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: [{}, {}, {}] }
])

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

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及其列表中获取所有ID included_lists个ID。然后像我的结果一样返回所有items返回。

此问题类似于:http://www.example.com/login.aspx,但由于含糊不清和缺乏数据库文件,我对此提出了疑问。

1 个答案:

答案 0 :(得分:1)

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

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()