简体   繁体   English

MongoDB - 与嵌套数组的聚合不起作用

[英]MongoDB - Aggregate with nested array not working

I have 2 collections using a simple nested array.我有 2 collections 使用简单的嵌套数组。 An example of the records looks like so:记录示例如下所示:

menus菜单

{ 
    "_id" : ObjectId("620323fe80ec16abea7a0205"), 
    "name" : "A new menu", 
    "description" : "", 
    "menuData" : [
        {
            "catName" : "Cat 2", 
            "items" : [
                "61ded42461b1d8966c5efc45", 
                "61ded55001e3cafb8db19198", 
                "61df9adf2441b6113033f341", 
                "61dfa8c82441b6113033f738"
            ]
        }, 
        {
            "catName" : "Cat 1", 
            "items" : [
                "62020691adda4aab89f1742d", 
                "61dfa8c82441b6113033f738"
            ]
        }
    ], 
    "status" : "active", 
}

and I'm trying to complete a lookup on the menu items array (menuData.items).我正在尝试完成对菜单项数组 (menuData.items) 的查找。 These are object IDs from another collection.这些是来自另一个集合的 object 个 ID。

menuitems One example is: menuitems一个例子是:

{ 
    "_id" : ObjectId("61ded55001e3cafb8db19198"), 
    "name" : "Coca-Cola 600ml", 
    "description" : "Refreshing taste...ahhh", 
    "price" : 3.95, 
    "tags" : [
        "drinks"
    ], 
    "options" : [

    ], 
    "status" : "active",  
}

Using the following aggregate, I do not get the output of the lookup使用以下聚合,我没有得到查找的 output

db.getCollection("menus").aggregate(
[
    { 
        "$match" : { 
            "_id" : ObjectId("620323fe80ec16abea7a0205")
        }
    }, 
    { 
        "$addFields" : { 
            "newField" : "$menuData.items"
        }
    }, 
    { 
        "$unwind" : { 
            "path" : "$newField"
        }
    }, 
    { 
        "$lookup" : { 
            "from" : "menuitems", 
            "localField" : "newField", 
            "foreignField" : "_id", 
            "as" : "items"
        }
    }
]
);

Output Output

{ 
    "_id" : ObjectId("620323fe80ec16abea7a0205"), 
    "name" : "A new menu", 
    "description" : "", 
    "menuData" : [
        {
            "catName" : "Cat 2", 
            "items" : [
                "61ded42461b1d8966c5efc45", 
                "61ded55001e3cafb8db19198", 
                "61df9adf2441b6113033f341", 
                "61dfa8c82441b6113033f738"
            ]
        }, 
        {
            "catName" : "Cat 1", 
            "items" : [
                "62020691adda4aab89f1742d", 
                "61dfa8c82441b6113033f738"
            ]
        }
    ], 
    "status" : "active", 
    "createdBy" : ObjectId("61bb07c778e39ca45c161d81"), 
    "createdByModel" : "Administrator", 
    "createdAt" : ISODate("2022-02-09T02:16:30.108+0000"), 
    "updatedAt" : ISODate("2022-02-09T04:26:00.837+0000"), 
    "__v" : NumberInt(0), 
    "newField" : [
        "61ded42461b1d8966c5efc45", 
        "61ded55001e3cafb8db19198", 
        "61df9adf2441b6113033f341", 
        "61dfa8c82441b6113033f738"
    ], 
    "items" : [

    ]
}

As you can see, items are empty.如您所见,项目是空的。 I tried to complete this without $addFields and $unwind - with the same result.我尝试在没有$addFields$unwind的情况下完成此操作 - 结果相同。

Any help is greatly appreciated.任何帮助是极大的赞赏。

You need $lookup with pipeline .你需要$lookup with pipeline

  1. Converting ObjectId to string ( newFields hold set of Id strings while menuItems document Id is an ObjectId )将 ObjectId 转换为字符串( newFields保存一组 Id 字符串,而menuItems文档 Id 是一个ObjectId
  2. Check the converted id string is within the newFields array via $in .通过$in检查转换后的id字符串是否在newFields数组中。
{
  "$lookup": {
    "from": "menuitems",
    let: {
      newField: "$newField"
    },
    pipeline: [
      {
        $match: {
          $expr: {
            $in: [
              {
                $toString: "$_id"
              },
              "$$newField"
            ]
          }
        }
      }
    ],
    "as": "items"
  }
}

Sample Demo on Mongo Playground Mongo Playground 上的示例演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM