[Learning Note] [MongoDB] aggregate group project - Gukie/building-recommend GitHub Wiki

refer: https://docs.mongodb.com/v3.2/reference/operator/aggregation/

group building data by plate field and output the avgPrice not null row.

db.building.aggregate(
    [
       {
            $group:{
               _id:"$plate",
                avgPrice:{
                        $avg:{
                               
                               $cond:[
                                    {$gt:["$avgPrice",0]},
                                    "$avgPrice",
                                    "empty"
                                ]
                        }
                },
                count:{$sum:{
                        $cond:[
                                {$gt:["$avgPrice",0]},
                                1,
                                0
                              ]
                    }}
            }
           
        },
        {
         $project:{
                    _id:0 ,   
                    plate: "$_id",                
                    avgPrice:{$ceil:"$avgPrice"}                      

                }    
        },
        {
          $match:{
                avgPrice:{$ne:null}
              }
        },
        {
           $sort:{
               avgPrice:-1
               }
            }
    ]
     
)