MongoDB Aggregation Pipeline Explained Simply
The Aggregation Pipeline is like a step-by-step process to filter, modify, and analyze data in MongoDB. Think of it as an assembly line, where data goes through different stages to get the desired result.
Each stage in the pipeline modifies or processes the data before passing it to the next stage.
- $match (Filter Documents Like WHERE in SQL) 🔹 Selects only the documents that match a specific condition. 🔹 Similar to SQL's WHERE clause.
Example: Get users who live in "New York"
js
Copy
Edit
db.users.aggregate([
{ $match: { city: "New York" } }
])
- $group (Group Data Like GROUP BY in SQL) 🔹 Groups multiple documents together based on a field. 🔹 Useful for counting, summing, and averaging data.
Example: Count users in each city
js
Copy
Edit
db.users.aggregate([
{ $group: { _id: "$city", totalUsers: { $sum: 1 } } }
])
✅ _id → Grouping by city
✅ totalUsers → Counts users in each city
- $limit (Limit Number of Results) 🔹 Stops processing after a certain number of results. 🔹 Like LIMIT in SQL.
Example: Get only the first 3 users
js
Copy
Edit
db.users.aggregate([
{ $limit: 3 }
])
- $sort (Sort Data Like ORDER BY in SQL) 🔹 Sorts the data in ascending (1) or descending (-1) order.
Example: Get users sorted by age (oldest first)
js
Copy
Edit
db.users.aggregate([
{ $sort: { age: -1 } } // Descending order
])
- $project (Select Specific Fields Like SELECT in SQL) 🔹 Used to show or hide specific fields. 🔹 Similar to SELECT column_name in SQL.
Example: Show only name and age, hide _id
js
Copy
Edit
db.users.aggregate([
{ $project: { name: 1, age: 1, _id: 0 } }
])
✅ 1 → Show field
✅ 0 → Hide field
- $addFields (Add New Fields or Modify Existing Fields) 🔹 Creates new fields or updates existing ones.
Example: Add a new field fullName
js
Copy
Edit
db.users.aggregate([
{ $addFields: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
])
- $count (Count Total Documents) 🔹 Counts the number of documents after filtering.
Example: Count users in "New York"
js
Copy
Edit
db.users.aggregate([
{ $match: { city: "New York" } },
{ $count: "totalUsers" }
])
- $lookup (Join Two Collections Like JOIN in SQL) 🔹 Joins data from another collection (like SQL JOIN). 🔹 Useful when you have related data in different collections.
Example: Join users and orders collections
js
Copy
Edit
db.users.aggregate([
{
$lookup: {
from: "orders", // Collection to join
localField: "_id", // Field in users
foreignField: "userId", // Matching field in orders
as: "userOrders" // New array field with matching orders
}
}
])
✅ Finds all orders where userId in orders matches _id in users.
✅ The matching orders are added as an array called "userOrders".
- $out (Store Results in a New Collection) 🔹 Saves the result of the aggregation into a new collection.
Example: Save user count per city into cityStats collection
js
Copy
Edit
db.users.aggregate([
{ $group: { _id: "$city", totalUsers: { $sum: 1 } } },
{ $out: "cityStats" }
])
✅ Now, a new collection cityStats is created with grouped data.
Example: Complete Pipeline
Let's say we want to:
1️⃣ Find users older than 25
2️⃣ Group them by city and count them
3️⃣ Sort by the number of users (most to least)
4️⃣ Limit the result to the top 3 cities
js
Copy
Edit
db.users.aggregate([
{ $match: { age: { $gt: 25 } } }, // Step 1: Filter users
{ $group: { _id: "$city", count: { $sum: 1 } } }, // Step 2: Group by city
{ $sort: { count: -1 } }, // Step 3: Sort by count descending
{ $limit: 3 } // Step 4: Limit to top 3
])
Summary Table
Operator Purpose SQL Equivalent
$match Filters data WHERE
$group Groups data GROUP BY
$limit Limits results LIMIT
$sort Sorts data ORDER BY
$project Selects specific fields SELECT column_name
$addFields Adds/updates fields ALTER TABLE (Not exactly the same)
$count Counts documents COUNT(*)
$lookup Joins another collection JOIN
$out Saves results in a new collection INSERT INTO new_table
Final Thoughts
MongoDB Aggregation Pipeline is a powerful tool for analyzing and transforming data. The key is understanding each stage and combining them effectively. 🚀
https://chatgpt.com/share/67ce9027-eb30-8005-93c0-aaa2d04d439c