MongoDB Aggregate()

Looks like a project of mine will be utilizing MongoDB, and this is just a whole new ball game to me… I cant for the life of me get my head around the aggregate function, all the examples I find seem to be off my learning style.

Assuming I have SOME_ID and SOME_COUNT in a “document” or a row, how would I write the equivelent:


select
some_id, sum(some_count)
from
myCollection
group by
some_id

After taking lunch I tried again with success:


db.myCollection.aggregate([{$group: {_id: "$some_id", count: {$sum: 1}}}]);

I don’t fully understand it but hey… Now I need to figure out how to add in $where…

I… think (and i could be wrong - it’s been all of 30 seconds reading the aggregation documentation) that it would be something along the lines of…

db.myCollection.aggregate({$match: {field : "value"}},{$group: {_id: "$some_id", count: {$sum: 1}}});

Yeah I got it figured out, I’ll put some examples here later. My biggest problem was the syntax as I had never seen JSON style before and if you don’t know it, it can be very hard to stop problems.

Not sure if this would be of any help, but I used to use http://jsonviewer.codeplex.com/ when I needed to verify the json I received or created was valid. Maybe there is something better for MongoDB out there, but if not, this will provide some help.

Ah cool thank you! I think I’m getting the hang of it, and it looks like JSON / Mongo might be my life for awhile :slight_smile: