Course contentsShow
← Databases & SQL
Lesson 1192 of 1,95311. NoSQL and Document ModelsFree lesson

Aggregating Document Streams

Build multi-stage pipelines to transform and group data for reporting.

In this lesson, you will learn how to build multi-stage pipelines to transform and group raw document streams into structured, high-level reports.

The Concept of the Pipeline

When working with document databases, data often arrives in "streams"—a continuous flow of individual JSON or BSON documents. If you need to generate a summary report (e.g., "Total sales per category per month"), you cannot simply query the table like a relational database. Instead, you treat your collection as a stream that passes through a series of specialized processing stages.

Think of it like an assembly line in a factory. Each stage performs one specific action: filtering out unwanted documents, reshaping the fields, or grouping them based on shared characteristics.

Transforming and Grouping

To aggregate effectively, you use an ordered sequence of operations. For example, imagine a collection of orders. To calculate total revenue by status, your pipeline would look something like this:

db.orders.aggregate([
  // Stage 1: Filter out canceled orders
  { $match: { status: { $ne: "canceled" } } },
  
  // Stage 2: Group by status and sum the total
  { 
    $group: { 
      _id: "$status", 
      totalRevenue: { $sum: "$price" } 
    } 
  }
])
  1. Filtering ($match): This reduces the volume of data moving down the line, ensuring that later, more expensive operations (like grouping) only handle the necessary documents.
  2. **Transforming/Grouping (group):∗∗Thisstageactsasabottleneckwhereindividualdocumentslosetheiruniqueidentityandareconsolidatedbasedonan‘id‘key.The‘group):** This stage acts as a bottleneck where individual documents lose their unique identity and are consolidated based on an `_id` key. The `sum` operator here performs the math as the stream flows through this "bucket."

By chaining these stages, you can transform messy, document-based logs into clean business intelligence. You aren't just querying; you are sculpting the data as it flows through the pipeline until only the desired report remains. This approach is highly efficient for read-heavy workloads because it allows you to compute complex results on-the-fly without needing to pre-process every document in your application code.

Key Takeaway: Aggregating document streams is the process of piping data through sequential, specialized stages—filtering, reshaping, and grouping—to convert raw, individual documents into actionable summary reports.