【问题标题】:Get min value from array of object using aggregate and lookup mongodb使用聚合和查找 mongodb 从对象数组中获取最小值
【发布时间】:2021-12-09 15:45:37
【问题描述】:

我有两个集合propertiesproperty_prices,关系是一个属性多个价格。所以我想加入他们,然后从property_prices.monthly_unit_price.unit_price 中找到最小值。所以我可以从整个房产定价中获得房产及其价格和最小单位价格值。

Property Collection

{
    "_id": "1",
    "status": "Approved",
    "name": "My Property Lake"
}

Property Price Collection 其中monthly_unit_price 包含1-12 月的对象

{
    "property_prices": [
        {
            "property_id": "1",
            "block_id": "ABC",
            "monthly_unit_price": [{ "month": "Jan", "unit_price": 100 }, { "month": "Dec", "unit_price": "1200" }],
        },
        {
            "property_id": "1",
            "block_id": "DEF",
            "monthly_unit_price": [{ "month": "Jan", "unit_price": "200" }, { "month": "Dec", "unit_price": "2400" }],
        }
    ]
}

基本上我想从 property_prices unit_price 获取 property_id 1 的最小值

所以我尝试使用aggregatelookup,但我无法从property_prices 获得整个属性的最小值。 这是我尝试过的

await Property.aggregate([
    {
        $lookup: {
          from: 'property_prices',
          as: 'property_prices',
          let: { property_id: '$_id' },
          pipeline: [
            {
                $match: {
                    $expr: {
                    $and: [
                        { $eq: ['$property_id', '$$property_id'] },
                        { $eq: ['$status', 'Completed'] },
                    ]
                    }
                }
            },
            
          ]
        },
    },
    {
        $unwind: "$property_prices"
    },
    {
        $group: {
            _id: '$property_prices.property_id',
            minInvestment: { "$min": "$property_prices.monthly_unit_price.unit_price" }
        }
    },
]);

我期待的结果是

{
    "_id": "1",
    "status": "Approved",
    "name": "My Property Lake",
    "property_prices": [
        {
            "property_id": "1",
            "block_id": "ABC",
            "monthly_unit_price": [{ "month": "Jan", "unit_price": 100 }, { "month": "Dec", "unit_price": "1200" }],
        },
        {
            "property_id": "1",
            "block_id": "DEF",
            "monthly_unit_price": [{ "month": "Jan", "unit_price": "200" }, { "month": "Dec", "unit_price": "2400" }],
        }
    ],
    "minInvestment":100
}

【问题讨论】:

    标签: mongodb mongoose nosql aggregate


    【解决方案1】:

    您在正确的轨道上,您只需要稍微“按摩”文档结构,因为它是一个嵌套数组。这是一个使用 $map$reduce 运算符的快速示例。

    请注意,我还必须使用 $toInt 将值转换为数字类型,我建议在更新/插入时处理这些事情。

    db.properties.aggregate([
      {
        $lookup: {
          from: "property_prices",
          as: "property_prices",
          let: {
            property_id: "$_id"
          },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [
                    {
                      $eq: [
                        "$property_id",
                        "$$property_id"
                      ]
                    },
                    {
                      $eq: [
                        "$status",
                        "Completed"
                      ]
                    }
                  ]
                }
              }
            }
          ]
        }
      },
      {
        $addFields: {
          minInvestment: {
            $min: {
              $reduce: {
                input: {
                  $map: {
                    input: "$property_prices",
                    as: "property",
                    in: {
                      $map: {
                        input: "$$property.monthly_unit_price",
                        as: "price",
                        in: {
                          $toInt: "$$price.unit_price"
                        }
                      }
                    }
                  }
                },
                initialValue: [],
                in: {
                  "$concatArrays": [
                    "$$value",
                    "$$this"
                  ]
                }
              }
            }
          }
        }
      }
    ])
    

    Mongo Playground

    【讨论】:

    • 非常感谢第一次尝试就成功了...
    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 2019-02-28
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多