【问题标题】:how to calculate avg, median, min, max in mongodb query?如何在mongodb查询中计算平均值、中位数、最小值、最大值?
【发布时间】:2019-06-25 18:00:07
【问题描述】:

我有ListPrice 字段以该价格收集我必须计算所有数据的最小值、最大值、中值、平均值,有效standardStatus,售出standardStatus

我尝试使用聚合和 for 循环进行计算,但它不起作用

        db.collection('selected_properties').aggregate([
        { presentation_id : ObjectId(req.body.presentation_id),
          checked_status : true}
        },
        {
            $lookup : { from :'properties', localField : 'property_id', foreignField : '_id', as : 'property_info'}
        },
        {
            $unwind : {path : '$property_info', preserveNullAndEmptyArrays : true}
        },

        {
            $sort : {'property_info.ListPrice' : 1}
        },
        {
            $group:{
                 _id: "$user_id",
               minActiveListPrice: { $min: { $cond: [ {                 
                       $eq: [ "$property_info.StandardStatus", "A" ]}, 
                              '$property_info.ListPrice',''  ] } },
               maxActiveListPrice: { $max: { $cond: [ {
                        $eq: [ "$property_info.StandardStatus", "A" ]}, 
                               '$property_info.ListPrice',0 ] } },
               avgActiveListPrice: { $avg: { $cond: [ {
                        $eq: [ "$property_info.StandardStatus", "A" ]},
                                '$property_info.ListPrice',''  ] } },
               medianActiveListprice: { $push: { $cond: [ {
                        $eq: [ "$property_info.StandardStatus", "A" ]},                        
                               '$property_info.ListPrice',''  ] } },                                        
               minsoldListPrice: { $min: { $cond: [ {
                        $eq: [ "$property_info.StandardStatus", "S" ]},
                              '$property_info.ListPrice',''  ] } },
               maxsoldListPrice: { $max: { $cond: [ {
                        $eq: [ "$property_info.StandardStatus", "S" ]},
                              '$property_info.ListPrice',0  ] } },
               avgsoldListPrice: { $avg: { $cond: [ {
                        $eq: [ "$property_info.StandardStatus", "S" ]},
                              '$property_info.ListPrice',''  ] } },

                 avgPrice: { $avg: "$property_info.ListPrice" },
                 maxPrice: { $max: "$property_info.ListPrice" },
                 minPrice: { $min: "$property_info.ListPrice" },
              }
               median: { $push:  "$property_info.ListPrice"}                         
            }
         },

【问题讨论】:

标签: mongodb


【解决方案1】:
db.collection('selected_properties').aggregate([
        {
            $match : { presentation_id : ObjectId(req.body.presentation_id),
                      checked_status : true}
        },
        {
            $lookup : { from :'properties', localField : 'property_id', 
                        foreignField : '_id', as : 'property_info'}
        },
        {
            $unwind : {path : '$property_info', preserveNullAndEmptyArrays : true}
        },

        {
            $sort : {'property_info.ListPrice' : 1}
        },
        {
            $group:
              {
                _id: "$user_id",
                minActiveListPrice: { $min: { $cond: [ {                 
                         $eq: [ "$property_info.StandardStatus", "A" ]}, 
                                '$property_info.ListPrice',''  ] } },
                maxActiveListPrice: { $max: { $cond: [ {
                         $eq: [ "$property_info.StandardStatus", "A" ]}, 
                                '$property_info.ListPrice',0 ] } },
                avgActiveListPrice: { $avg: { $cond: [ {
                         $eq: [ "$property_info.StandardStatus", "A" ]},
                                '$property_info.ListPrice',''  ] } },
                medianActiveListprice: { $push: { $cond: [ {
                         $eq: [ "$property_info.StandardStatus", "A" ]},
                                '$property_info.ListPrice',null ] } },

                avgPrice: { $avg: "$property_info.ListPrice" },
                maxPrice: { $max: "$property_info.ListPrice" },
                minPrice: { $min: "$property_info.ListPrice" },
                median: { $push:  "$property_info.ListPrice"}                         
            }
        },
        { "$project": {
            "minActiveListPrice":1,
            "maxActiveListPrice":1,
            "avgActiveListPrice":1, 
            "avgPrice": 1,
            "maxPrice": 1,
            "minPrice": 1,
            "medianActiveListpricevalue": {
                $let: {
                    vars: {
                       arr: { $filter: {
                                input: "$medianActiveListprice",
                                as: "aa",
                                cond: {$ne:["$$aa",null]}
                            }},

                    },
                    in: {  "$cond": {
                        "if": {
                            "$eq": [{$mod: [ {$size:"$$arr"}, 2 ]}, 0]
                        },
                        "then": {
                            $avg:[ 
                            { $arrayElemAt: [ "$$arr", {$subtract:[{$divide: [ {$size:"$$arr"}, 2 ]},1]}]},
                            { $arrayElemAt: [ "$$arr", {$divide: [ {$size:"$$arr"}, 2 ]}]}
                            ]
                        },
                        "else": {
                            $arrayElemAt: [ "$$arr",{$floor : {$divide: [ {$size:"$$arr"}, 2 ]}}]
                       }
                            }}
                 }
            },

            "medianvalue":{  "$cond": {
                "if": {
                  "$eq": [{$mod: [ {$size:"$median"}, 2 ]}, 0]
                }
                "then": {
                    $avg:[ 
                    { $arrayElemAt: [ "$median", {$subtract:[{$divide: [ {$size:"$median"}, 2 ]},1]}]},
                    { $arrayElemAt: [ "$median", {$divide: [ {$size:"$median"}, 2 ]}]}
                    ]
                },
                "else": {
                     $arrayElemAt: [ "$median",{$floor : {$divide: [ {$size:"$median"}, 2 ]}}]
                }
            }}
        } }
    ])

【讨论】:

  • 这是对您问题的回答吗?如果不是,请改为编辑您的问题并从答案部分删除补充。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
相关资源
最近更新 更多