MongoDB Aggregation pipelines
What are mongoDB aggregation pipelines ?
MongoDB’s Aggregation Pipeline is a framework used to process data and perform operations on collections. It allows you to transform, filter, and aggregate data in a collection using a series of stages, each with a specific operation.
The pipeline consists of multiple stages. Each stage processes the input documents and produces output, which becomes the input for the next stage.
It can perform filtering, grouping, sorting, projecting, and even complex transformations on documents.
Let’s look pipelines with examples.
Before proceeding kindy inject the attached (objects) data in MongoDB MongoDB Data sets like below and push it to mongo cluster.
- Basis Structure looks like
db.collection.aggregate([
{ <stage1>: { <operation1> } },
{ <stage2>: { <operation2> } },
...
]);
Common Aggregation Stages
$match & $count
Match filters documents based on a condition, similar to the find query & count does the total of documents.
- For eg -
How many users are active & count them?
- For eg -
[
{
$match: {
isActive:true
}
},
{
$count: 'Total Active Users'
}]
OP
$group & $avg
This basically groups documents by a specified key (id) and performs operations like sum, average, etc whereas avg calculates the average for document
For eg -
What are average age of users ?
$sort & $limit
Sorts documents based on a field & -1 means in descending order whereas 1 mean to ascend.
Limits perform the number of documents returned.
Eg -
List 4 common fruits among users and show only top 5?
Eg -
Find total no of male and females ?
Eg -
Which country has highest no of registered users?
$unwind
Deconstructs an array field into individual documents.
Eg -
What is average no of tags per users ?
Alternate pipeline to find average.
Eg - How many users have enim as one of their tags ?
- Alternate pipleline below also we can follow.
[
{
$match: {
tags: "enim",
}
},
{
$count: 'Users With Enim Tags'
}]
$project
It basically reshapes or filter the documents by including, excluding, or creating new fields.
Eg -
What are the names and age of users who are inactive and have velit as tag ?
Eg - How many users have a phone no starting with +1(940) ?
Eg - Who has registered the most recently ?
$push :
- We basically use this operator to add elements to an array field. It is commonly used in the context of the update operation and the Aggregation Pipeline to append values to an existing array.
Eg -
Categorize users by their favorite fruit.
Eg -
How many users have ad as the second tag in their list of tags ?
Eg -
Find users who have both enim and id as their tags ?
Eg - List all the companies located in USA with corresponding user count
$lookup:
- Performs a left outer join with another collection.