Aggregation Stages - MongoDB Aggregation Tutorial for beginner

Leave a Comment
In Last tutorial we have seen basic concept of mongodb aggregation. In this tutorial we gonna take tour of mongodb aggregation stages.

For this whole tutorials series i am gonna use ROBO 3T to execute mongodb query. you can download ROBO 3T software from this website https://robomongo.org/ and download database from this google drive link.

Mongodb Aggregation without any stages

In Previous blog we have seen syntax of mongodb aggregate function so let's execute simple aggregate query on our student collection.
db.getCollection("person").aggregate([]);

Mongodb Aggregation without any stages

In above query we pass blank array as an argument to mongodb aggregate and as a result we get all document of collection it's means without any stages aggregate produce same result like mongodb find method with empty query. let's execute find method of mongodb.
db.getCollection("person").find({});

Mongodb Find method

If you see both query produce same result. Now let's talk about aggregation stages.

Aggregation Stages

Let's discuses more deeper what is aggregation stage? which stage are exist in mongodb aggregation framework. Each stage work independently, each stage take input of document then perform its operation and produce output document. Some stages produce same document as input document like sort and limit stages or it can be produce new document when we use group stages. Let's look stages operator

Aggregation Stages Operators

{ $<stageoperator> : {} }
Each stage start from stage operator append with $ sign then comes object, object contain key value pair let's see and example
{ $match : {score: {$gt:20} } }
{ $sort : { count: -1} }
In above example we can see match and sort stage shown, we will discuses each stage operator individually later in this tutorials. For now you just remember how to construct stage. Below are some mongodb aggregation stages with quick overview.

  • $match: It is use filter document based on certain query, this help to reduce document which are gonna pass to next stage.
  • $group: Group document using certain criteria.
  • $project: Filter fields in documents.
  • $unwind: Unwind takes an array field and separate each array fields in to documents.
  • $sort: Sorts the documents.
  • $count: Count number of objects documents.
  • $limit: Limit number of documents.
  • $skip: Skip certain amount of documents.
  • $out: Out writes aggregation result in to another collections.


Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .

0 comments:

Post a Comment

Powered by Blogger.