

Unwinding works on array here in our collection we have array of subjects (which consists of different subjects inside it like math, physics, English, etc) so unwinding will be done on that i.e.

$project: It is used to select some specific fields from a collection.

$match: It is used for filtering the documents can reduce the amount of documents that are given as input to the next stage.Stages: Each stage starts from stage operators which are: Here, the aggregate() function is used to perform aggregation it can have three operators stages, expression and accumulator. In the Second Stage, the $group stage groups the documents by the id field to calculate the sum of fare for each unique id. class: “first-class” and passes the document to the second stage. Here, the $match stage filters the documents by the value in class field i.e. In the above example of a collection of train fares in the first stage. Let us discuss the aggregation pipeline with the help of an example: You can also use the aggregation pipeline in sharded collection. The basic pipeline stages provide filters that will perform like queries and the document transformation modifies the resultant document and the other pipeline provides tools for grouping and sorting documents. Or in other words, the aggregation pipeline is a multi-stage pipeline, so in each state, the documents taken as input and produce the resultant set of documents now in the next stage(id available) the resultant documents taken as input and produce output, this process is going on till the last stage. In MongoDB, the aggregation pipeline consists of stages and each stage transforms the document. MongoDB provides three ways to perform aggregation ISRO CS Syllabus for Scientist/Engineer Exam.ISRO CS Original Papers and Official Keys.GATE CS Original Papers and Official Keys.The syntax is very much similar to the field selector used in querying: we can select fields to project by specifying "fieldname”: 1 or exclude fields with "fieldname”: 0. This projects the author (by) field in each document. Sort the authors by the occurrence count, descending.Įach of these steps maps to an aggregation framework operator. Group the authors (by), counting the number of occurrences.ģ. Project the authors (by) out of each book document.Ģ. Assuming that each book is stored as a document in MongoDB, we could create a pipeline with multiple steps:ġ. We might want to find out the author (by) who has the highest number of documents (books). Basically, we build a pipeline that processes a stream of documents through several building blocks: filtering, projecting, grouping, sorting, limiting, and skipping.Įxample: We have a book collection under the library database. The aggregation framework helps us to transform and combine documents in a collection.
