MongoDB for AI Application Engineering Handbook · PRACTICAL GUIDE

Aggregation Pipeline for AI Features

Transform MongoDB events into summaries, rankings and reusable business signals for AI features through readable, testable aggregation pipeline stages.

HANDBOOK JOURNEYByte 4 of 5View all Bytes
HANDBOOK JOURNEYByte 4 of 5

MongoDB for AI Application Engineering Handbook

25 min focused reading
  1. BYTE 01Document Model Basics for AI Apps
  2. BYTE 02Atlas Vector Search and RAG with MongoDB
  3. BYTE 03Storing Conversation and Agent Memory in MongoDB
  4. 05BYTE 05Production Concerns for MongoDB AI Apps
FAMILIAR SCENARIO

Sort receipts into a useful summary

A shop filters receipts, groups sales by category and totals them before preparing a report.

01Filter
02Group
03Calculate
04Report

Connect the idea: Aggregation stages turn raw records into business signals.

MONGODB HANDBOOK 04

What you will learn

Transform MongoDB events into summaries, rankings and reusable business signals for AI features through readable, testable aggregation pipeline stages.

Quick Start

AI features often need summaries such as counts, averages, recent activity and ranked signals. In this Byte, we build those features directly from MongoDB documents using the aggregation pipeline. No SQL background or earlier handbook is required.

Real-World Scenario

Rahul, who previously built fraud-detection features using SQL window functions, now needs the same kind of feature — "average transaction amount per customer over the last 30 days" — but PaisaWise's transaction log for the mobile app lives in MongoDB. He asks Divya: "Does MongoDB even have something like SQL's GROUP BY and window functions?" Divya says: "It does — it's called the aggregation pipeline, and it's arguably even more expressive."

Core Explanation

In relational analytics, aggregation means grouping rows and computing a summary value (like AVG() or SUM()) per group. MongoDB's aggregation pipeline does the same job, but structured as a sequence of clearly-named stages that data flows through — like an assembly line, where each station ($match, $group, $sort, and so on) transforms the data a bit further before passing it to the next station.

This "pipeline of stages" structure is often easier to read than a deeply nested SQL query, since each stage is a small, understandable step you can reason about (and even test) independently.

Architecture and Flow Diagram

AGGREGATION PIPELINEEach stage transforms the result for the next stage
MONGODB
$matchPaid enrolments
$groupRevenue by course
$projectUseful fields
$sortHighest first
Filter early so later stages process fewer documents.

Computing "average transaction amount per customer, last 30 days" — an AI feature:

JAVASCRIPT
01db.transactions.aggregate([02  {03    $match: {04      timestamp: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }05    }06  },07  {08    $group: {09      _id: "$customer_id",10      avg_transaction_amount: { $avg: "$amount" },11      transaction_count: { $sum: 1 }12    }13  },14  {15    $sort: { avg_transaction_amount: -1 }16  }17])

Compare each stage to its SQL Handbook equivalent: $match is your WHERE clause, $group with $avg and $sum is your GROUP BY with aggregate functions, and $sort is your ORDER BY — same underlying logic, expressed as a readable sequence of steps.

A window-function-style feature (rolling behavior per customer) using $setWindowFields:

JAVASCRIPT
01db.transactions.aggregate([02  {03    $setWindowFields: {04      partitionBy: "$customer_id",05      sortBy: { timestamp: 1 },06      output: {07        running_total: { $sum: "$amount", window: { documents: ["unbounded", "current"] } }08      }09    }10  }11])

This closely mirrors the relational SUM() OVER (PARTITION BY ... ORDER BY ...) pattern — MongoDB's $setWindowFields stage is its window function equivalent.

Hands-On Concept Lab

PIPELINE BUILDER

Run each stage and watch data become a feature

INTERACTIVE
SELECTED VIEW$match Step 1 of 3
01Input: 2,400 events02Keep: completed lessons03Output: 860 events

What changed?Filter early so later stages process less data.

Feeding aggregation output directly into a feature document for a fraud model:

JAVASCRIPT
01db.transactions.aggregate([02  { $match: { customer_id: "cust_5521" } },03  {04    $group: {05      _id: "$customer_id",06      avg_amount_30d: { $avg: "$amount" },07      txn_count_30d: { $sum: 1 },08      max_amount_30d: { $max: "$amount" }09    }10  },11  {12    $merge: { into: "customer_features", whenMatched: "replace" }13  }14])

The final $merge stage writes the computed features directly into a customer_features collection — ready for a fraud-detection model to read, without a separate export/import step.

Real-World Industry Context

MongoDB's own recent engineering work reflects how central this kind of in-database computation has become: MongoDB 8.3 specifically moved "common transformations into the database to eliminate external pipelines," alongside performance gains of 30% more complex operations compared to the previous release. This matters directly for AI feature engineering — computing features inside the database, close to the data, avoids the latency and complexity of exporting raw data to a separate processing system just to compute a handful of aggregate statistics.

Common Mistakes

  1. Running aggregations without an index on the fields used in $match or $sort — just like an unindexed SQL WHERE clause, this can force MongoDB to scan far more documents than necessary.
  2. Building an enormous single pipeline instead of testing stages incrementally — since each stage's output feeds the next, it's much easier to debug a pipeline by checking the result after each stage individually first.
  3. Forgetting $merge writes data, not just returns it — unlike most aggregation stages, $merge has a real side effect (writing to a collection); using it experimentally on a production collection without care can overwrite live data.
  4. Recomputing the same aggregation on every single request — for expensive features (like 30-day rolling averages), consider precomputing and storing them (as in S5), rather than running the full aggregation on every API call.

Persona Recap

Rahul feels at home quickly: "Once I saw $match/$group/$sort line up with WHERE/GROUP BY/ORDER BY, this stopped feeling foreign." Meena asks: "So we could compute all our fraud features this way, right inside MongoDB?" Divya: "Exactly — and with $merge, those features land directly in a collection ready for the model to consume, no separate export pipeline needed."

Comparison

ConceptSQL HandbookMongoDB Aggregation Pipeline
Filtering rowsWHERE$match
Grouping + aggregate functionsGROUP BY + AVG()/SUM()$group with $avg/$sum
Ordering resultsORDER BY$sort
Window functionsSUM() OVER (PARTITION BY ...)$setWindowFields
Writing results to a new table/collectionINSERT INTO ... SELECT$merge

Practice Task

Design (on paper) an aggregation pipeline that computes, per customer, the number of failed transactions in the last 7 days — a feature that might feed into a fraud-risk score. Name the stages you'd use, in order, and what each one does.

Key Takeaways

  • MongoDB's aggregation pipeline does the same job as SQL's GROUP BY/aggregate functions, structured as a readable sequence of named stages data flows through.
  • $match, $group, and $sort map almost directly onto SQL's WHERE, GROUP BY, and ORDER BY.
  • $setWindowFields provides window-function-style calculations (running totals, rolling averages) equivalent to SQL's OVER (PARTITION BY ...).
  • $merge writes computed features directly into a collection, letting AI models consume precomputed features without a separate export step.
  • MongoDB is actively optimizing for in-database computation, aiming to eliminate the need for external pipelines just to compute common features.

FAQ and Knowledge Check

Q1: What MongoDB aggregation stage is equivalent to SQL's WHERE clause? $match — it filters documents before they proceed to later stages.

Q2: What MongoDB stage provides window-function-style calculations like running totals? $setWindowFields, equivalent to SQL's SUM() OVER (PARTITION BY ... ORDER BY ...) pattern.

Q3: Why might you precompute and store features with $merge instead of running the aggregation on every request? Expensive aggregations (like 30-day rolling averages) are wasteful to recompute repeatedly; precomputing once and storing the result is far more efficient for frequent reads.

Knowledge Check:

  1. Name the three MongoDB aggregation stages equivalent to SQL's WHERE, GROUP BY, and ORDER BY.
  2. True/False: $merge only returns computed results without writing anything to the database.
  3. Why is it useful to test aggregation pipeline stages incrementally rather than all at once?

(Answers: 1. $match, $group, $sort; 2. False — $merge writes its output directly into a specified collection, a real side effect; 3. Because each stage's output feeds the next, so checking the result after each stage individually makes debugging much easier)

Next byte: Production Concerns for MongoDB AI Apps — indexing, scaling, and security as these systems go live.

Interactive Knowledge Check

LESSON CHECKPOINTConfirm the concept before moving forward

Choose an answer, inspect the explanation and explain the idea in your own words.

RETENTION
Learning rule: explain the answer in your own words before checking the next Byte.

Primary sources

OPTIONAL LEARNING CONNECTIONS

Continue by concept

Choose only what supports your next goal. This Byte does not require either link.