MongoDB Aggregation – Data Aggregation In MongoDB Tutorial

MongoDB Aggregation – Data Aggregation In MongoDB from Coding compiler. In this MongoDB tutorial, you will learn about MongoDB aggregation with examples. Let’s start learning data aggregation in MongoDB database. Learn Now.!

MongoDB Aggregation Pipeline

MongoDB aggregation pipelines provide a way to transform consolidated documents into collections. You can pass the documentation through the pipeline, just like the Unix “pipe”, passing the output of one command to the second, third, and so on.

Related MongoDB Tutorials

What is MongoDB Tutorial For Beginners
MongoDB Basics Tutorial
MongoDB Update Tutorial
MongoDB Query Tutorial
MongoDB Data Modelling Tutorial

The simplest aggregation should be something you are already familiar with in SQL  group by . We have already seen the  count() method, then let us know how many unicorns there are, how many unicorns are there?

db.unicorns.aggregate([{$group:{_id:'$gender',
    total: {$sum:1}}}])

In the shell, we have a  aggregate helper class that performs the pipeline operation of the array. For simple group counting of objects, we only need a simple call  $group.

This is GROUP BY exactly the same as in SQL  , we use to create a new document, with  _id fields indicating what we are grouping (here is  gender), and other fields are usually assigned as aggregated results, here we match Each document of a gender uses  $sum 1 .

You should notice that the  _id field is assigned  '$gender' instead of the  'gender' – in front of  '$' the field, which will be replaced by a value with the same name in the input document, a placeholder.

What other pipelines can we use? One of the  $group previous (and often used) ones is  $match – this  find is exactly the same as the method, allowing us to get a matching subset of the document, or to filter the document in our results.

db.unicorns.aggregate([{$match: {weight:{$lt:600}}},
    {$group: {_id:'$gender',  total:{$sum:1},
      avgVamp:{$avg:'$vampires'}}},
    {$sort:{avgVamp:-1}} ])

Here we introduce another pipeline operations  $sort , and the role you want exactly the same, as well, and together with it  $skip and  $limit. And use the $group operation  $avg.

MongoDB arrays are very powerful, and they don’t prevent us from writing to the saved array. We need to be able to “flatten” them so that we can count everything:

db.unicorns.aggregate([{$unwind:'$loves'},
    {$group: {_id:'$loves',  total:{$sum:1},
    unicorns:{$addToSet:'$name'}}},
    {$sort:{total:-1}}, 
    {$limit:1} ])

Here we can find out the food that the unicorns like most, and the list of foods that the unicorns like to eat. $sort And  $limit the combination allows you to get a result “top N” such queries.

There is another powerful pipeline operation called  $project (similar  find) that allows you to get the specified field and create or calculate a new field based on the existing field. For example, you can use mathematical operations to add several fields before doing the averaging operation, or you can create a new field with a string operation to stitch existing fields.

This is just the fur of the many functions that can be done with aggregation. The aggregation has more power, such as the aggregate command can return the cursor of the result set  or can write the result Go to another new collection and work through the  $out pipeline. You can  get more examples of pipeline operations and expression manipulation from the  MongoDB manual .

MapReduce

MapReduce performs data processing in two steps. The first is map, then reduce. In the map step, convert the input document and output a key=>value pair (key and / or value can be complicated). Then, the key/value pairs are grouped by key, and the values of the same key are added to an array. In the reduce step, get an array of the key and the value of the key to produce the final result. The map and reduce methods are written in JavaScript.

In MongoDB we use mapReduce commands for a collection  . mapReduce Execute the map method, reduce method and output instruction. In our shell we can create a JavaScript method for input. Many libraries support string methods (a bit ugly).

The third parameter sets an additional parameter, for example we can filter, sort and limit the documents we want to analyze. We can also provide a  finalize way to handle  reduce the results after the step.

In most of your aggregations, you may not need to use MapReduce, but if you want, you can read more about it, from here  MongoDB manual.

Summary

In this MongoDB tutorial we introduced MongoDB’s aggregation capabilities. Once you understand the construction of the Aggregation Pipeline, it is relatively easy to write, and it is a powerful tool for aggregating data.

Related MongoDB Tutorials For Beginners

What is MongoDB Tutorial For Beginners

MongoDB Basics Tutorial

MongoDB Update Tutorial

MongoDB Query Tutorial

MongoDB Data Modelling Tutorial

MongoDB Scenarios

Leave a Comment