This is a guest post from MongoLab
The aggregation framework, one of the most powerful and highly anticipated features in the forthcoming MongoDB 2.2 release, lets you construct a server-side processing pipeline to be run on a collection. A rich set of operations are available for incorporation in the pipeline so as to achieve various kinds of collection transforms, ranging from simple multi-document calculations (e.g., sums and averages) to complex projections and pivots.
- Create a 2.1 MongoLab database
- with your own unique name, say Instructions here. You'll need your mongod username and password.
- On your database's home page, copy the mongo shell connection to your clipboard.
git clone git://gist.github.com/1401585.git aggdemo ; cd aggdemo
- Edit articles.js and aggregation.js to use the your db <myaggdemo> (see below)
<your connection> mongo -u -p <mongod password> articles.js (inserts the data into your database, 3 documents)em>
mongo --shell <your connection> -u <mongod username> -p <mongod password> aggregation.js (performs several aggregation examples and leaves you in the mongo shell.)
- Type g1 in the mongo shell to see the first $group result discussed below.
*See the MongoDB blog post for more information on the aggregation framework
The results of the aggregation are saved to convenient variables for examination. The group operations (g1 and g5) at the end of the aggregation.js file are noteworthy because they rollup three operators into a common pivot and aggregation example. The g1 data flow is shown below. Click it for a larger version or here for a .pdf version.