I am trying to aggregate an events field. I am supposed to find the sum.
Here is a sample of the dataset.
{ "_id" : ObjectId("582db9b82de1b251ee2d60a9"), "uuid" : "4898009f81a3ee8a", "session_id" : "26102016181211", "project_token" : "5a26b01e-622d-4512-aba4-158651cd0846", "starttime" : ISODate("2016-10-26T23:46:07Z"), "activity_name" : "ProductInfoActivity", "content_id" : 987560, "endtime" : ISODate("2016-10-26T23:48:42Z"), "time_duration" : 86245, "motion" : "running", "timestamp" : "Wed Nov 16 11:44:54 +05:30 2016", "os" : "Android", "events" : [ { "Virality" : 3, "_id" : "Virality" } ] }
So far I have got to this query.
db.pageview.aggregate([{$match:{'project_token':"5a26b01e-622d-4512-aba4-158651cd0846",'content_id':987560}},{'$group':{ _id : '$events._id','total_Virality':{$sum:'$events.Virality'}}}])
It gives me a result as
{ "_id" : [ "Virality" ], "total_Virality" : 0 }
{ "_id" : null, "total_Virality" : 0 }
It is supposed to give me a 3 but it returns 0 instead.
Any help is appreciated.
db.pageview.aggregate([{$match:{'project_token':"5a26b01e-622d-4512-aba4-158651cd0846",'content_id':987560}},{'$unwind':'$events'},{'$group':{ '_id' : '$events._id','total_Virality':{$sum:'$events.Virality'}}}]). It works. You can add it as an answer.