MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

bagaimana cara menghitung rata-rata, median, min, maks dalam kueri mongodb?

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 ]}}]
                }
            }}
        } }
    ])



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Pengantar Data Musim Semi MongoDB

  2. Operasi pembaruan Mongodb di dalam sub array

  3. MongoError:gagal terhubung ke server pada koneksi pertama

  4. Cara mengecualikan _id tanpa menyertakan bidang lain menggunakan kerangka kerja agregasi

  5. Repositori mongodb data pegas ekspresi reguler