MongoDB $查找带有引用objectId的对象数组

问题描述 投票:0回答:3

我有订单集合,我正在从中获取数据,如下所示:

 [
  {
    "_id": "628216b7b30bb8aa80c8fd1a",
    "promotionsDetails": {
      "companyTotalPrice": 27,
      "promotionsData": [
        {
          "_id": "621de063bb5f9f0bf510897f",
          "price": 27,
          "companyId": "621dd85eb45ca2ae292d9a36"
        },
        {
          "_id": "621de063bb5f9f0bf510897d",
          "price": 19,
          "companyId": "621dd85eb45ca2ae292d9a32"
        }
      ]
    }
  },
  {
    "_id": "628214fcb30bb8aa80c8fd18",
    "promotionsDetails": {
      "companyTotalPrice": 46,
      "promotionsData": [
        {
          "_id": "621de063bb5f9f0bf510897f",
          "price": 46,
          "companyId": "621dd85eb45ca2ae292d9a32",
        }
      ]
    },
  }
]

我想做的是使用数组中每个对象中的companyId objectId 从公司集合中获取公司详细信息,如下所示:

    [
  {
    "_id": "628216b7b30bb8aa80c8fd1a",
    "promotionsDetails": {
      "companyTotalPrice": 27,
      "promotionsData": [
        {
          "_id": "621de063bb5f9f0bf510897f",
          "price": 27,
          "companyId": "621dd85eb45ca2ae292d9a36",
          "companyData": { "title": "..." }
        },
        {
          "_id": "621de063bb5f9f0bf510897d",
          "price": 19,
          "companyId": "621dd85eb45ca2ae292d9a32",
          "companyData": { "title": "..." }
        }
      ]
    }
  },
  {
    "_id": "628214fcb30bb8aa80c8fd18",
    "promotionsDetails": {
      "companyTotalPrice": 46,
      "promotionsData": [
        {
          "_id": "621de063bb5f9f0bf510897f",
          "price": 46,
          "companyId": "621dd85eb45ca2ae292d9a32",
          "companyData": { "title": "..." }
        }
      ]
    }
  }
]

我尝试使用查找和管道,但没有得到所需的结果,谢谢!

mongodb mongoose collections lookup
3个回答
5
投票

实际

$lookup
支持数组,所以不需要
$unwind
和改变结构。这将返回您预期的结果:

db.Orders.aggregate([
  {
    $lookup: {
      from: "Company",
      localField: "promotionsDetails.promotionsData.companyId",
      foreignField: "_id",
      as: "companyfullData"
    }
  },
  {
    $set: {
      "promotionsDetails.promotionsData": {
        $map: {
          input: "$promotionsDetails.promotionsData",
          in: {
            $mergeObjects: [
              "$$this",
              {
                companyData: {
                  $arrayElemAt: [
                    "$companyfullData",
                    {$indexOfArray: ["$companyfullData.id", "$$this.id"]}
                  ]
                }
              }
            ]
          }
        }
      }
    }
  },
  {$unset: "companyfullData"}
])

游乐场


1
投票

在这里,为了更清楚地看一下Mongo游乐场

db.Orders.aggregate([
  {
    $unwind: "$promotionsDetails.promotionsData"
  },
  {
    "$lookup": {
      "from": "Company",
      "localField": "promotionsDetails.promotionsData.companyId",
      "foreignField": "_id",
      "as": "promotionsDetails.promotionsData.companyData"
    }
  },
  
])


0
投票

值得注意的是,虽然获得最多支持的答案大部分都有效,但 $indexOfArray 与 $arrayElemAt 有一个不幸的交互。如果它在数组中找不到您要查找的值,它将返回

-1
,这将导致 $arrayElemAt 获取数组中的 last 元素,这将是不正确的,因为没有想要的值元素。

我会发表评论,但我没有足够的代表。

© www.soinside.com 2019 - 2024. All rights reserved.