Aggregation pipelines
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
$matchfirst so it can use an index and shrink the data. $groupwith$avgand$sumcomputes counts and averages.- Do the work in the database instead of loading documents into Java.
$lookupis a join; use sparingly, it is not a relational join engine.
Draft outline details
Demonstration
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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.