使用另一个ID数组(聚合$in)获取数组对象中的项 - MongoDB。

3

我在mongodb中有一个存储集合

[
{
    "_id" : ObjectId("6043adb043707c034d5363b7"),
    "shopId" : "shopid1",
    "appId" : "777",
    "shopItems" : [
        {
            "itemId" : 1, <-- mongodbId
            // ..another fields
        },
        {
            "itemId" : 2 <-- mongodbId
            // ..another fields
        }
    ]
}
{
    "_id" : ObjectId("6043adb043707c034d5363b7"),
    "shopId" : "shopId2",
    "appId" : "777",
    "shopItems" : [
        {
            "itemId" : "itemId" : 1, <-- mongodbId
            // ..another fields
        },
        {
            "itemId" : "itemId" : 3, <-- mongodbId
            // ..another fields
        }
    ]
}
]

我需要获取仅当shopItems中的itemId在另一个数组中时的文档。

我从产品集合中使用另一个查询来获取这些文档。

这是一个id数组 ['2', '3'] <- 这只是一个示例,实际上是mongodb对象Ids。

我的聚合查询已经正常工作:

const stores  = await Store.aggregate([
            { $match: query },
            { $unwind: "$shopItems" },
            {
                $lookup: {
                    from: "products",
                    localField: "shopItems.itemId",
                    foreignField: "_id",
                    as: "itemId"
                }
            },
            {
                $lookup: {
                    from: "colors",
                    localField: "shopItems.itemColor",
                    foreignField: "_id",
                    as: "itemColor"
                }
            },
            {
                $lookup: {
                    from: "sizes",
                    localField: "shopItems.itemSize",
                    foreignField: "_id",
                    as: "itemSize"
                }
            },
            {
                $lookup: {
                    from: 'shops',
                    localField: 'shopId',
                    foreignField: '_id',
                    as: 'shop'
                }
            },
            {$unwind: { path: '$shop', preserveNullAndEmptyArrays: true}},
            {
                $addFields: {
                    "shopItems.itemColor": { $arrayElemAt: ["$itemColor.colorName", 0] },
                    "shopItems.itemSize": { $arrayElemAt: ["$itemSize.sizeName", 0] },
                    "shopItems.itemName": { $arrayElemAt: ["$itemId.productName", 0] },
                    "shopItems.productArticle": { $arrayElemAt: ['$itemId.article', 0] },
                    "shopItems.productBarcode": { $arrayElemAt: ['$itemId.barCode', 0] },
                    "shopItems.shopName": "$shop.shopName",
                    "shopItems.shopId": "$shop._id",
                    "shopName": '$shop.shopName',
                }
            },
            {
                $group: {
                    _id: "$_id",
                    shopId: {$first: '$shop._id'},
                    shopItems: { $push: "$shopItems" },
                    shopName: { $first: '$shop.shopName' }
                }
            },
            {
                $project: {
                   shopId: 1,
                   shopName: 1,
                   shopItems: 1
                }
            }
        ])

因此,我需要输出以下数据:
{
    "_id" : ObjectId("6043adb043707c034d5363b7"),
    "shopId" : "shopid1",
    "appId" : "777",
    "shopItems" : [
        {
            **"itemId" : 2** <-- mongodbId
            // ..another fields
        }
    ]
}
{
    "_id" : ObjectId("6043adb043707c034d5363b7"),
    "shopId" : "shopId2",
    "appId" : "777",
    "shopItems" : [
        {
            **"itemId" : 3**, <-- mongodbId
            // ..another fields
        }
    ]
}
1个回答

1
如果我理解正确,您只需要在$project阶段中这样使用$filter
{
  $project: {
    _id: 1,
    shopId: 1,
    appId: 1,
    shopItems: {
      $filter: {
        input: "$shopItems",
        as: "items",
        cond: {
          $in: [
            "$$items.itemId",
            yourDesiredArray
          ]
        }
      }
    }
  }
}

示例 此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接