Database Reference
In-Depth Information
The Aggregation Framework
The aggregation framework in MongoDB represents the ability to perform a selection of operations on all of the
data in your collection. This is done by creating a pipeline of aggregation operations that will be executed in order first
on the data and then each subsequent operation will be on the results of the previous operation. Those of you familiar
with the Linux or Unix shell will recognize this as forming a shell pipeline of operations.
Within the Aggregation framework there are a plethora of operators, which can be used as part of your
aggregations to corral your data. Here we will cover off some of the high-level pipeline operators and run through
some examples on how to use these. This means that we will be covering off the following operators:
$group
$limit
$match
$sort
$unwind
$project
$skip
For further details about the full suite of operators, check out the aggregation documentation, available at
http://docs.mongodb.org/manual/aggregation/ . We've created an example collection you can use to test some of
the aggregation commands. Extract the archive with the following command:
$ tar -xvf test.tgz
x test/
x test/aggregation.bson
x test/aggregation.metadata.json
x test/mapreduce.bson
x test/mapreduce.metadata.json
The next thing to do is to run the mongorestore command to restore the test database:
$ mongorestore test
connected to: 127.0.0.1
Sun Jul 21 19:26:21.342 test/aggregation.bson
Sun Jul 21 19:26:21.342 going into namespace [test.aggregation]
1000 objects found
Sun Jul 21 19:26:21.350 Creating index: { key: { _id: 1 }, ns: "test.aggregation", name: "_id_" }
Sun Jul 21 19:26:21.688 test/mapreduce.bson
Sun Jul 21 19:26:21.689 going into namespace [test.mapreduce]
1000 objects found
Sun Jul 21 19:26:21.695 Creating index: { key: { _id: 1 }, ns: "test.mapreduce", name: "_id_" }
Now that we have a collection of data to work with, we need to look at how to run an aggregation command and how to
build an aggregation pipeline. To run an aggregation query we use the aggregate command and provide it a single document
that contains the pipeline. For our tests we will run the following aggregation command with various pipeline documents:
> db.aggregation.aggregate({pipeline document})
So, without further ado, let's start working through our aggregation examples.
 
Search WWH ::




Custom Search