如何使用 mongodb 聚合查找数组中每个对象的特定字段

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

我正在尝试从 rentalcontractdetails 表中查找每个 rentalContractId。产品文档看起来像这样:

{
  "productPrice": [
    {
      "rentalContractId": ObjectId("64018dfc8a7d37f378d6c427"),
      "price": 20
    },
    {
      "rentalContractId": ObjectId("64018dfc8a7d37f378d6c426"),
      "price": 10
    }
  ]
}

我只需要查找rentalContractId。所以在聚合中执行以下查询以获得该输出,

db.products.aggregate([
{
      $lookup: {
        from: "rentalcontractdetails",
        localField: "productPrice.rentalContractId",
        foreignField: "_id",
        as: "rentalContract",
      },
    }, { $set: {
      "productPrice.rentalContract": "$rentalContract"
    }}])

但这正在产生这样的输出。 “产品价格”:

[
                    {
                        "rentalContractId": "64018dfc8a7d37f378d6c427",
                        "price": 20,
                        "rentalContract": [
                            {
                                "_id": "64018dfc8a7d37f378d6c427",
                                "contractType": "8 - Days Rental",
                                "contractDaysInNumber": 8,
                                "createdAt": "2023-03-03T06:04:44.531Z",
                                "isDeleted": false,
                                "isArchived": false
                            },
                            {
                                "_id": "64018dfc8a7d37f378d6c426",
                                "contractType": "4 - Days Rental",
                                "contractDaysInNumber": 4,
                                "createdAt": "2023-03-03T06:04:44.531Z",
                                "isDeleted": false,
                                "isArchived": false
                            }
                        ]
                    },
                    {
                        "rentalContractId": "64018dfc8a7d37f378d6c426",
                        "price": 10,
                        "rentalContract": [
                            {
                                "_id": "64018dfc8a7d37f378d6c427",
                                "contractType": "8 - Days Rental",
                                "contractDaysInNumber": 8,
                                "createdAt": "2023-03-03T06:04:44.531Z",
                                "isDeleted": false,
                                "isArchived": false
                            },
                            {
                                "_id": "64018dfc8a7d37f378d6c426",
                                "contractType": "4 - Days Rental",
                                "contractDaysInNumber": 4,
                                "createdAt": "2023-03-03T06:04:44.531Z",
                                "isDeleted": false,
                                "isArchived": false
                            }
                        ]
                    }
                ]

预期输出:

{
  "productPrice": [
    {
      "rentalContractId": {
          "_id": "64018dfc8a7d37f378d6c427",
          "contractType": "8 - Days Rental",
        },
      "price": 20
    },
    {
      "rentalContractId": {
          "_id": "64018dfc8a7d37f378d6c426",
          "contractType": "4 - Days Rental",
        }
      "price": 10
    }
  ]
}

提前致谢。

mongodb mongoose aggregation-framework nosql-aggregation
1个回答
0
投票

一个选择是将

$lookup
与管道一起使用,并更新您的
$set
步骤以在
$mergeObjects
中使用
$map

db.products.aggregate([
  {$lookup: {
      from: "rentalcontractdetails",
      localField: "productPrice.rentalContractId",
      foreignField: "_id",
      as: "rentalContract",
      pipeline: [{$project: {contractType: 1}}]
  }},
  {$project: {
      _id: 0,
      productPrice: {$map: {
          input: "$rentalContract",
          in: {$mergeObjects: [
              {rentalContractId: "$$this"},
              {price: {$arrayElemAt: [
                    "$productPrice.price",
                    {$indexOfArray: ["$productPrice.rentalContractId", "$$this._id"]}
              ]}}
          ]}
      }}
  }}
])

游乐场示例中查看它是如何工作的

  • mongodb
    $lookup
    和 mongoose
    populate
    是两个不同的东西,使用两种不同的机制。最初的问题是将两者混合在一起,但在代码中使用了
    $lookup
    ,所以我想这就是意图。
© www.soinside.com 2019 - 2024. All rights reserved.