Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

aggregation-pipelines-advanced

Advanced aggregation stages

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Advanced aggregation stages: document modeling, query operators, and aggregation patterns for MongoDB.

Aggregation pipelines power analytics in-place—$match early and index-backed filters appear in every data interview.

You will apply Advanced aggregation stages in contexts like: In-app dashboards, funnel metrics, and ETL-lite without exporting every row to a warehouse.

Copy JavaScript shell queries from each lesson into mongosh or MongoDB Atlas Data Explorer—inspect matched documents and explain plans. The in-browser lab (execution_profile: none) ships later; mongosh is the practice path now. Also put $match as early as possible in the pipeline.

When find, operators, and basic aggregation pipelines feel familiar in mongosh.

$set/$addFields, $replaceRoot, $merge, and window-like patterns with $setWindowFields (version-dependent).

addFields

db.orders.aggregate([
  { $addFields: { tax: { $multiply: ['$total', 0.2] } } },
  { $addFields: { grandTotal: { $add: ['$total', '$tax'] } } }
])

Practice: Run aggregation pipelines in mongosh.

$merge to collection

db.orders.aggregate([
  { $match: { status: 'shipped' } },
  { $group: { _id: '$customerId', revenue: { $sum: '$total' } } },
  { $merge: { into: 'customer_revenue', on: '_id', whenMatched: 'replace', whenNotMatched: 'insert' } }
])

Materializes analytics collection—document merge permissions in production.

Important interview questions and answers

  1. Q: $merge vs $out?
    A: $out replaces target collection; $merge upserts with rules.
  2. Q: $facet use?
    A: Multiple reports in one aggregation command.

Self-check

  1. What does $addFields do?
  2. When use $merge?

Pitfall: $merge permissions—test on staging before materializing prod analytics.

Interview prep

$merge?
Writes pipeline output into collection with merge rules.
$addFields?
Adds computed fields without replacing doc.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • $merge vs $out?
  • $addFields?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump