Mongodb Muliple averages based on a different column -


using mongodb aggregate, there way have query return weight average on scale , average of scale 1 , average of scale 2 all returned in same query?

this example of entry in data set

{     "profile" : "p1",    "avgweight" : 639,    "time" : "2017-04-14t05:17:42.000z",    "scale" : 1,    "weight" : 1504,    "target" : 680 } 

my query running averaging weight accrost scales ( might not help, better have more info )

 [{                     "$match": {                         "time": {                             "$gt": moment(start).format("yyyy-mm-dd hh:mm:ss"),                             "$lt": moment(end).format("yyyy-mm-dd hh:mm:ss")                         }                     }                 },                 {                     "$group": {                         "_id": {                             "hour": {                                 "$hour": "$time"                             },                             "day": {                                 "$dayofyear": "$time"                             },                             "interval": {                                 "$add": [{                                         "$multiply": [{                                             "$minute": "$time"                                         }]                                     },                                     {                                         "$multiply": [{                                                 "$hour": "$time"                                             },                                             100                                         ]                                     },                                     {                                         "$multiply": [{                                                 "$dayofyear": "$time"                                             },                                             10000                                         ]                                     },                                     {                                         "$multiply": [{                                                 "$year": "$time"                                             },                                             10000000                                         ]                                     }                                 ]                             }                         },                         "time": {                             "$first": "$time"                         },                         "avgw": {                             "$avg": "$avgweight"                         },                         "avgwe": {                             "$avg": "$weight"                         },                         "avgtarget": {                             "$avg": "$target"                         }                     }                 }, {                     "$sort": {                         "time": -1                     }                 }             ] 

adding expected response like

[   {     "_id : {"hour":1,"day":105,"interval":20971050122},  "time":"2017-04-15t01:22:58.000z",     "avgw":646,     "avgwe":1577,     "avgtarget":680 ,      "scale1" : 100 ,      "scale2" : 120      } ,    { "_id":{"hour":1,"day":105,"interval":20771050122},    "time":"2017-04-15t01:22:55.000z",     "avgw":646,     "avgwe":1335,     "avgtarget":680 ,      "scale1" : 100 ,      "scale2" : 120 }  ] 

but if little different can handle it, long scales in same parent object ( cpu intensive post process them link matching groups )

you can split first group 2 groups.

first group calculate weight avg scales , second group rest of avgs.

something like:

 [{         "$match": {             "time": {                 "$gt": moment(start).format("yyyy-mm-dd hh:mm:ss"),                 "$lt": moment(end).format("yyyy-mm-dd hh:mm:ss")             }         }     }, {         "$group": {             "_id": {                 "scale": "$scale",                 "hour": {                     "$hour": "$time"                 },                 "day": {                     "$dayofyear": "$time"                 },                 "interval": {                     "$add": [{                             "$multiply": [{                                 "$minute": "$time"                             }]                         },                         {                             "$multiply": [{                                     "$hour": "$time"                                 },                                 100                             ]                         },                         {                             "$multiply": [{                                     "$dayofyear": "$time"                                 },                                 10000                             ]                         },                         {                             "$multiply": [{                                     "$year": "$time"                                 },                                 10000000                             ]                         }                     ]                 }             },             "time": {                 "$first": "$time"             },             "scaleavg: {             "$avg": "$weight"         }     }  }, {     "$group": {         "_id": {             "hour": "$_id.hour",             "day": "$_id.day",             "interval": "$_id.interval"         },         "time": {             "$first": "$time"         },         "avgw": {             "$avg": "$avgweight"         },         "avgwe": {             "$avg": "$weight"         },         "avgtarget": {             "$avg": "$target"         },         "scaleavgs": {             "$push": {                 "scale": "$_id.scale",                 "scaleavg": "$scaleavg"             }         }     }  }, {     "$sort": {         "time": -1     }  }] 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -