Last active
March 11, 2016 16:48
-
-
Save eMahtab/940c3c602a268665bbc5 to your computer and use it in GitHub Desktop.
Mastering MongoDB Aggregation Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MongoDB comes with a great aggregation framework, which allows to write queries to fetch whatever data you might be interested into. | |
Before we even get into MongoDB aggreagtion and how to use it, first question is why we even need aggreagtion framework when we already have MongoDB query language and operations like find(), findOne(), limit(), skip(), sort(). The answer is aggregation framework is built into MongoDB to provide more complex analytics oriented functionality. | |
Lets dive into MongoDB aggregation framework and see what it have to offer. | |
Before getting into details of the aggreagtion framework lets see what all stages are available in MongoDB aggreation framework. | |
1. $match | |
2. $project | |
3. $skip | |
4. $sort | |
5. $limit | |
Without any further ado lets use MongoDB aggregation framework. | |
1. Find all the companies which were founded in year 2004 ? | |
db.companies.aggregate([ | |
{$match:{founded_year:2004}} | |
]) | |
Note that we can write the above query without using aggregation framework | |
db.companies.find({founded_year:2004}) | |
2. Find all the companies which were founed in 2004 and display only their name and founded_year (project only name and founded_year field) ? | |
db.companies.aggregate([ | |
{$match:{founded_year:2004}}, | |
{$project:{_id:0,name:1,founded_year:1}} | |
]) | |
Again you can achieve the same thing withput using MongoDB aggregation framework | |
db.companies.find({founded_year:2004},{_id:0,name:1,founded_year:1}) | |
3. Lets use the $limit stage to limit the number of records | |
db.companies.aggregate([ | |
{$match:{founded_year:2004}}, | |
{$limit:5}, | |
{$project:{_id:0,name:1,founded_year:1}} | |
]) | |
Once again you can achieve the same without using aggregation framework. | |
db.companies.find({founded_year:2004},{_id:0,name:1,founded_year:1}).limit(5) | |
4. Next lets use the $sort stage to sort the documents | |
db.companies.aggregate([ | |
{$match:{founded_year:2004}}, | |
{$limit:5}, | |
{$sort:{name:1}}, | |
{$project:{_id:0,name:1}} | |
]) | |
Note that below MongoDB query will result in a different result than aggregation query as in the below query sort will run first and then the limit. | |
db.companies.find({founded_year:2004},{_id:0,name:1}).limit(5).sort({name:1}) | |
So above query is equivalent to below MongoDB aggregate query | |
db.companies.aggregate([ | |
{$match:{founded_year:2004}}, | |
{$sort:{name:1}}, | |
{$limit:5}, | |
{$project:{_id:0,name:1}} | |
]) | |
So be very careful in the order in which you specify your aggregation stages in aggregation pipeline. | |
5. Now lets use the $skip stage to skip some documents | |
db.companies.aggregate([ | |
{$match:{founded_year:2004}}, | |
{$skip:10}, | |
{$limit:5}, | |
{$sort:{name:1}}, | |
{$project:{_id:0,name:1}} | |
]) | |
Lets try to write a query without aggreagtion to get the same result as above. | |
db.companies.find({founded_year:2004},{_id:0,name:1}).skip(10).limit(5).sort({name:1}) | |
Note that above query will return different set of result, because it will do the sort first then skip and then limit. | |
Now you should be able to appreciate the power of MongoDB Aggreagtion framework and its intutiveness while writing complex queries. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment