What it feels like to live in a more equal society

Currently I live in one of the most equal places on the planet. Whilst I personally do not agree with all these rankings but when you look at how it fares in the various measurements — be it to…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Aggregation Framework In MongoDB

MongoDB is an open source document-oriented database. These types of databases are highly flexible, allowing structure variations in the documents, and MongoDB can save even documents partially. With lots of powerful features and advantages, the Aggregation Framework is one of the most powerful features of MongoDB.

In MongoDB, the Aggregation framework is used to process data records/documents and return computed results. We can group documents and perform various operations, use join within a database, merge collection, and many more exciting things.

Let’s begin with an aggregation framework; to use aggregation, it’s required to use the pipeline. Aggregation pipeline consists of stages, and at each stage, the document is passed sequentially, transformed/output documents of the stage served to next stage as input and this process goes until the last stage.

To write/initiate aggregation, run the following mongo shell command

db.getCollection(collectionName).aggregate(pipeline)

where collectionName is the name of the collection on which aggregation is performed and pipeline is an array of stages known as aggregation pipeline.

Aggregation pipeline stages can be single or multiple.

Some of most common and popular pipeline stage operators which are used :

Let’s discuss and explore some aggregation pipeline with the help of example:

Sample collections :

Lookup on objects array having objectId as key (Frogein key)

Let’s breakdown our query and discuss each stage in some details

Stage 1 :

By using the $unwind operator, we simply deconstruct the input array books and create output documents for each element in the books array.

Stage 2 :

$lookup operator , in this step we are data from another collection i.e books, as $lookup performs left outer join on collection in the same database and outputs document array against alias.

Stage 3 :

In this stage , we are again going to use $unwind but field is different i.e $books.book.

At this stage we have get data for books for each author documents.

Stage 4 :

Stage 5 :

This stage is pretty straightforward, in this we are fetching data of author again.

Stage 6 :

In this stage , $addFields is used to put books array from root level to inside authorAndBookDetails.

Stage 7 :

In this stage , we are replacing all others existing fields with authorAndBookDetails and makes it as root document.

And here is our desired result

Conclusion

In this article, we have seen some of various stage operators in aggregation pipeline to retrieve desired result.Beyond this aggregation has many more operators which makes it more flexible and powerful, which let you reshape documents, unpacking nested structures and regrouping them as needed.

Please feel free to provide any feedback in below comment section

Add a comment

Related posts:

How to simulate a laser pointer with your phone

options to simulate laser pointer with a smart phone

How to Make Meaningful Progress on a Big Goal

When it comes to breaking down a gigantic goal into actionable, measurable steps, things can feel a bit daunting. To quote Carl Sagan: At times, I’ve struggled to find the best starting point…