Priceless.
db.games.aggregate([
{ $match : { date : { $gt : ISODate("1999-08-01T00:00:00Z"), $lt : ISODate("2000-08-01T00:00:00Z") }}},
{ $unwind : '$teams' },
{ $match : { 'teams.won' : 1 }},
{ $group : { _id : '$teams.name', wins : { $sum : 1 }}},
{ $sort : { wins : -1 }},
{ $limit : 5 }
]);However, unlike a relational engine which uses a query plan, you need to actually tell the aggregation engine in which order to process the data.
Doesn't this seem a little... backward? If the dataset is complex enough, the two things that come to mind are:
1. There would be a lot of redundancy in the hierarchical dataset.
and
2. That seems like a lot of futzing around to get the most optimal aggregation path!
What am I missing here?
p.s. I do tend to agree that the SQL syntax is rather more readable, but then again I've been looking at SQL for a lot longer than I have JSON style queries. The first query is not that unreadable!
1. http://thecodebarbarian.wordpress.com/2014/02/14/crunching-3...