Skip to main content
InterviewHubPRO
Section Overview
Q13

Question 13 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

26%
MongoDB & NoSQL (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

How does the Aggregation Pipeline process data?

senior LevelCode ExamplePipesCI/CD & DevOpsSystem Design

In Simple Words: The Aggregation Pipeline is a multi-stage data processing framework modeled after Unix pipes (|):

  • Documents enter through the first stage; each stage transforms documents and passes output to the next stage.
  • Key Stages:
    • $match: Filters documents (place at the beginning to utilize indexes!).
    • $project: Reshapes, includes, or excludes fields.
    • $group: Groups documents by key and calculates metrics ($sum, $avg).
    • $unwind: Deconstructs an array field, outputting a document for every element.
    • $sort, $skip, $limit: Paginates results.
javascript
// Calculate total revenue per country for completed orders:
db.orders.aggregate([
  { $match: { status: "COMPLETED" } },
  { $group: { _id: "$country", totalRevenue: { $sum: "$amount" } } },
  { $sort: { totalRevenue: -1 } },
  { $limit: 5 }
]);