javascript - Multiple groups with mongodb aggregate -


i'm trying use mongodb aggregate on data:

"_id": objectid("598dbd301ab6476e5b15e05e"), "updated_at": isodate("2017-08-11t14:20:32.865z"), "created_at": isodate("2017-08-11t14:20:32.865z"), "action": objectid("59760749a398cb323cf1c051"), "subaction": objectid("5980c3807a8cb300110d87d3"), "person": objectid("598dbd2f1ab6476e5b15e05b"), "session": objectid("598dbd2f1ab6476e5b15e05c"), "dateaccomplish": isodate("2017-08-11t14:20:32z"), "createdby": objectid("595f8426645bf5f47366fb29"), "updatedby": objectid("595f8426645bf5f47366fb29"), 

what i'm trying need retrieve 2 groups. has grouped actions , subactions.

the output data expected looks this:

movactions: [ {      _id: $created_at,      count: ?,      data:[          {              _id: "$action",              count: 3,              data: [                 {                     _id: "$subaction",                     count: 2                 }              ]           }      ] }, ] 

there many subactions have action, want aggregate each action children subactions listed

to group multiple fields, $group should applied multiple times compound _id, gradually reducing in each next group:

db.collection.aggregate([     {$group:{         _id:{created_at:"$created_at", action:"$action", subaction:"$subaction"},         count: {$sum:1}     }},     {$group:{         _id:{created_at:"$_id.created_at", action:"$_id.action"},         count: {$sum:1},         data: {$push:{_id: "$_id.subaction", count:"$count"}}     }},     {$group:{         _id:"$_id.created_at",         count: {$sum:1},         data: {$push:{_id: "$_id.action", count:"$count", data:"$data"}}     }},      {$project: {         _id:0,         created_at:"$_id",          count:1,          data:1     }} ]); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -