i have following collection...
[{
"_id":"abcd1234",
"Date" :{"$date":1430418601000},
"Count":{
"hr1":0,
"hr2":20,
"hr3":1,
"hr4":4,
"hr5":0,
}
},
{
"_id":"abcd1234",
"Date" :{"$date":1430505001000},
"Count":{
"hr1":2,
"hr2":15,
"hr3":15,
"hr4":0,
"hr5":1,
}
}
]
I want to aggregate count for these dates so total count will be like...
{"hr1":2,
"hr2":35,
"hr3":16,//......so on
}
Is it possible in mongodb? I don't have much knowledge of mongo, if someone can guide me with this query will be helpful...
Running the following aggregation pipeline should give you the desired result:
db.collection.aggregate([
{
"$group": {
"_id": null,
"hr1": { "$sum": "$Count.hr1" },
"hr2": { "$sum": "$Count.hr2" },
"hr3": { "$sum": "$Count.hr3" },
"hr4": { "$sum": "$Count.hr4" },
"hr5": { "$sum": "$Count.hr5" }
}
}
])
Specifying an _id
value of null
will calculate accumulated values for all the input documents as a whole.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.