Skip to content
Ameen Abozaid
Planned lessonMID-042-L04

Aggregation pipelines

  • Lesson 4 of 7
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

compute summaries in the database.

Planned topics

  • A pipeline is a list of stages; each passes documents to the next: $match, $group, $sort, $project, $lookup.
  • Put $match first so it can use an index and shrink the data.
  • $group with $avg and $sum computes counts and averages.
  • Do the work in the database instead of loading documents into Java.
  • $lookup is a join; use sparingly, it is not a relational join engine.
Draft outline details

Demonstration

js
db.reviews.aggregate([
  { $match: { productId: 42 } },
  { $group: { _id: "$rating", count: { $sum: 1 } } },
  { $sort: { _id: -1 } }
])

Check your understanding

Why put $match first?

Show answer

It uses an index and reduces the documents later stages process.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.