Aggregation Pipeline

An aggregation pipeline consists of one or more stages that process documents:
Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values.
The documents that are output from a stage are passed to the next stage.
An aggregation pipeline can return results for groups of documents. For example, return the total, average, maximum, and minimum values.
In this blog, we will solve some questions to understand aggregation. For videos, visit Hitesh Chaudhary.
The sample data can be found in this gist: https://gist.github.com/hiteshchoudhary/a80d86b50a5d9c591198a23d79e1e467
Fill the data in a MongoDB Cluster[use mongodb extension for vscode] and use the aggregation tab to practice in the website itself.
Questions
- How many users are active?
[
{
$match: {
isActive: true,
}
},
{
$count: 'isActiveCount'
}
]
- What is the average age of all users?
[
{
$group: {
_id: null,
averageAge: {
$avg: "$age"
}
}
}
]
- List the top 3 common fruits among the user.
[
{
$group: {
_id: "$favoriteFruit",
count: {
$sum: 1
}
}
}, {
$sort: {
count: -1
}
}, {
$limit: 3
}
]
- Find the total number of male and females.
[
{
$group: {
_id: "$gender",
count: {
$sum: 1
}
}
}
]
# -------------------------- OR --------------------------
[
{
$group: {
_id: "$gender",
count: {
$count: {}
}
}
}
]
- Which country has the highest number of registered users?
[
{
$group: {
_id: "$company.location.country",
count: {
$count: {}
}
}
},
{
$sort: {
count: -1
}
},
{
$limit: 1
}
]
- List all unique eye colors present in the collection.
[
{
$group: {
_id: "$eyeColor"
}
}
]
- What is the average number of tags per user
[
{
$unwind: {
path: "$tags",
}
}, {
$group: {
_id: "$_id",
count: {
$sum: 1
}
}
}, {
$group: {
_id: null,
averageTagsCount: {
$avg: "$count"
}
}
}
]
# -------------------------- OR --------------------------
[
{
$addFields: {
tagsLength: {
$size: "$tags"
}
}
}, {
$group: {
_id: null,
averageTagsCount: {
$avg: "$tagsLength"
}
}
}
]
- How many users have ‘enim’ as one of their tags.
[
{
$match: {
tags: "enim"
}
}, {
$group: {
_id: null,
count: {
$sum: 1
}
}
}
]
- What are the name and ages of users who are inactive and have ‘velit’ as a tag?
[
{
$match: {
tags: "velit",
isActive: false
}
},
{
$project: {
name: 1,
age: 1
}
}
]
- How many users have a phone number starting with ‘+1 (940)’?
[
{
$match: {
"company.phone": /^\+1 \(940\)/
}
}, {
$count: 'count'
}
]
- Who has registered the most recently?
[
{
$sort: {
registered: -1
}
}, {
$limit: 1
}
]
- Categorize users by their favorite fruit and list the users in each category.
[
{
$group: {
_id: "$favoriteFruit",
users: {
$push: "$name"
}
}
}
]
- How many users have ‘ad’ as the second tag in their list of tags?
[
{
$match: {
"tags.1": "ad"
}
}
]
- Find users who have both ‘enim’ and ‘id’ as their tags?
[
{
$match: {
"tags": {
$all: ["enim", "id"]
}
}
}
]
- List all the companies location in the USA with their corresponding user count.
[
{
$match: {
"company.location.country": "USA"
}
}, {
$group: {
_id: "$company.title",
count: {
$sum: 1
}
}
}, {
$sort: {
count: -1
}
}
]
- Lookup
[{
$lookup: {
from: "authors",
localField: "author_id",
foreignField: "_id",
as: "author_details"
}
}, {
$addFields: {
author_details: {
$arrayElemAt: ["$author_details", 0]
}
}
}]
Subscribe to my newsletter
Read articles from Md Sohail Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Md Sohail Ansari
Md Sohail Ansari
Final Year Undergrad at IIIT Bhagalpur and a Full Stack Web Developer. Portfolio: https://www.heysohail.me/