BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Group By in MongoDB

| by Jonathan Allen Follow 7 Followers on Feb 17, 2012. Estimated reading time: 1 minute |

In response to demands for simpler data access, MongoDB is introducing the Aggregation Framework. This library uses a declarative pipe-line notation to support features such as SQL-like group by operations without having to write custom JavaScript routines.

After the “aggregate” clause, most pipeline operations begin with a “$match” clause. Together these act like SQL’s from and where clauses or MongoDB’s find function. Up next is the “$project” clause, which again looks a lot like its SQL or MongoDB counterpart (though unlike SQL it is properly located at the end of the expression).

The next operation is rather unique to MongoDB’s Aggregation Framework. Unlike most relational databases, MongoDB can natively store arrays within a row/document. While convenient for all-or-nothing access, this can make writing reports that need to use a combination of projections, grouping, and filtering quite complicated. The “$unwind” clause breaks down the array into single elements, which are then returned along with the rest of the document.

The “$group” operation serves the same purpose as SQL’s Group By clause, but works more like the LINQ grouping operators. Instead of getting back a flat row, the result set continues to be a nested structure. This allows it to return aggregate information such as counts and averages along with the actual documents, in whole or in part, that make up each grouping.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT