【问题标题】:$lookup aggregation for nested array object嵌套数组对象的 $lookup 聚合
【发布时间】:2021-08-09 16:46:16
【问题描述】:

我有 category 集合,其中包含子类别嵌入对象。我需要编写聚合脚本来首先从这个集合中查找到storelisting 集合。然后将商店列表字段添加到相应的子类别中。

类别

{
  "_id": "",
  "categoryName": "",
  "subCategories": [
    {
      "subCategoryId": "",
      "subCategoryName": "",
      "storeListingIds": [
        "1",
        "2"
      ]
    },
    {
      "subCategoryId": "",
      "subCategoryName": "",
      "storeListingIds": [
        "3","4","5"
      ]
    }
  ],
  "order": 2,
  "createdAt": {
    "$date": "2020-12-01T22:26:11.669Z"
  },
  "updatedAt": {
    "$date": "2021-04-27T17:17:25.442Z"
  },
  "_class": ""
}

商店列表

{
  "_id": "1",
  "storeListingName": "",
  "storeListingUrl": "",
  "catalogueIds": [
    ""
  ],
  "_class": ""
},
{
  "_id": "2",
  "storeListingName": "",
  "storeListingUrl": "",
  "catalogueIds": [
    ""
  ],
  "_class": ""
},
{
  "_id": "3",
  "storeListingName": "",
  "storeListingUrl": "",
  "catalogueIds": [
    ""
  ],
  "_class": ""
},
{
  "_id": "4",
  "storeListingName": "",
  "storeListingUrl": "",
  "catalogueIds": [
    ""
  ],
  "_class": ""
},
{
  "_id": "5",
  "storeListingName": "",
  "storeListingUrl": "",
  "catalogueIds": [
    ""
  ],
  "_class": ""
}

我需要在集合上方查找表单到 storeListing 集合。我也想要 storeListing 的所有字段。

结果应该是这样的:

{
  "_id": "",
  "categoryName": "",
  "subCategories": [
    {
      "subCategoryId": "",
      "subCategoryName": "",
      "storeListingIds": [
        "1",
        "2"
      ],
      "storeListings":[
        {
          "_id": "1",
          "storeListingName": "",
          "storeListingUrl": "",
          "catalogueIds": [
            ""
          ],
          "_class": ""
        },
        {
          "_id": "2",
          "storeListingName": "",
          "storeListingUrl": "",
          "catalogueIds": [
            ""
          ],
          "_class": ""
        }
      ]
    },
    {
      "subCategoryId": "",
      "subCategoryName": "",
      "storeListingIds": [
        "3","4","5"
      ],
      "storeListings":[
        {
          "_id": "3",
          "storeListingName": "",
          "storeListingUrl": "",
          "catalogueIds": [
            ""
          ],
          "_class": ""
        },
        {
          "_id": "4",
          "storeListingName": "",
          "storeListingUrl": "",
          "catalogueIds": [
            ""
          ],
          "_class": ""
        },
        {
          "_id": "5",
          "storeListingName": "",
          "storeListingUrl": "",
          "catalogueIds": [
            ""
          ],
          "_class": ""
        }
      ]
    }
  ],
  "order": 2,
  "createdAt": {
    "$date": "2020-12-01T22:26:11.669Z"
  },
  "updatedAt": {
    "$date": "2021-04-27T17:17:25.442Z"
  },
  "_class": ""
}

【问题讨论】:

  • 这个问题还不够清楚。你需要加入哪个收藏? storeListingName等人怎么来的?
  • @varman 我们有两个收藏品类和 storelisting,我们需要加入 category 和 storelisting。要加入这一点,我们需要检查“subCategories.storeListingIds”中的 id。 storeListingName 属性来自 storeListing 集合。这是 storelisting 集合的文档: { "_id": "4", "storeListingName": "", "storeListingUrl": "", "catalogueIds": [ "" ], "_class": "" }

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

说明

  1. 我们对嵌套的storeListingId 执行$lookup 并将结果存储在tmp 字段中
  2. 我们应用过滤并将subCategories 对象与storeListings 值合并

注意:我们之所以使用$mergeObjects是为了避免命名subCategories中的每个字段

$map: {
    input: "$subCategories",
    as: "subCat",
    in: {
        subCategoryId:"$$subCat.subCategoryId",
        subCategoryName:"$$subCat.subCategoryName",
        storeListingIds: "$$subCat.storeListingIds",
        storeListening: {
            $filter: {
                input: "$tmp",
                cond: {
                  $in: [ "$$this._id", "$$subCat.storeListingIds" ]
                }
            }
        }
    }
}

试试这个:

db.category.aggregate([
  {
    $lookup: {
      from: "storelisting",
      localField: "subCategories.storeListingIds",
      foreignField: "_id",
      as: "tmp"
    }
  },
  {
    $addFields: {
      tmp: "$$REMOVE",
      subCategories: {
        $map: {
          input: "$subCategories",
          as: "subCat",
          in: {
            "$mergeObjects": [
              "$$subCat",
              {
                storeListings: {
                  $filter: {
                    input: "$tmp",
                    cond: {
                      $in: [ "$$this._id", "$$subCat.storeListingIds" ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])

MongoPlayground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2020-04-03
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多