MongoDB Aggregation Pipeline and difference with Map-Reduce

Map-Reduce is a common way while using big data. It’s a way to extract and aggregate from a huge dataset. From version 2.2 our favorite database MongoDB introduced a new feature ‘Aggregation Pipeline’. It has same functionality as map reduce but it’s much faster than map reduce.

Let’s take an example (taken from MongoDB official docs) to understand the Aggregation Pipeline:
Say we have the below 4 documents in a collection orders.

Now we want to get the total amount of documents with status:"A" and group by cust_id.

The output will be

Now to get same output using map-reduce, the syntax is:

The aggregate command operates only on a single collection
Pipeline Operators:
$match : It filters the document based on the criteria given. For the above example filtering condition is status: “A” . It is always better to use $match as the first stage of the pipeline.
$sort : It shorts the matched document.
{ $sort: { field1: -1, field2: 1 } }
1 for ascending order.
-1 for descending order.

SQL to aggregation Mapping

WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT() $sum
join $lookup

For Map-Reduce please refer beginners-guide-to-mongodb-map-reduce

Arijit Naskar

Arijit Naskar is a Software Developer and QA, also founder of alltechstories.com a popular tech tutorial & how-to website since 2014. He loves to explore new technologies and writing blogs.

You may also like...