I have this document in mongodb:
{ "_id":"26/04/2015 09:50", "reservations":130, "Event_types":[ { "type":"Party", "events":[ { "eventName":"After Party", "total_count":130, "by":[ { "siteName":"club8", "countArray":[ { "bucket":"default", "value":40 } ] }, { "siteName":"PostParty", "countArray":[ { "bucket":"1", "value":70 }, { "bucket":"2", "value":20 } ] } ] } ] } ] }
What i'm looking for
I want to sum the "value" field and the group by these fields:
- of type
- event_name
- Hostname
So, for the document that I have, I would expect to get:
- For the combination {"Party", "After the party", "club8"} the amount is 40
- For the combination {"Party", "After Party", "PostParty"} the amount of 90
What i tried
I tried using an aggregate statement with a compound key for _id:
db.testing.aggregate( { $group : { _id : { type:'$Event_types.type', name: '$Event_types.events.eventName', siteName: '$Event_types.events.by.siteName' } , total : { $sum : '$Event_types.events.by.countArray.value' } } });
results
one document with three arrays - one for each value that I want to group. The array "siteName" contains 2 values ββavailable for "siteName". The βsumβ does not seem to sum anything, and it appears only once β I expected to see it next to each βSiteNameβ value in the document.
{ "_id":{ "type":[ "Party" ], "name":[ [ "After Party" ] ], "siteName":[ [ [ "club8", "PostParty" ] ] ] }, "total":0 }
Am I using "aggregate" in the wrong way, or is the scheme I'm using not suitable for my purpose? Thanks.