Demo: building the reviews service on MongoDB
On This Lesson 3 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
build the reviews service with a ratings summary. Prepares MID-042-LAB1.
Draft outline details
Step by step
- Create the
Reviewdocument and repository; addPOST /products/{id}/reviewsandGETfor the latest 20. - Add the compound index
{ productId: 1, createdAt: -1 }and proveIXSCANwithexplain. - Add
GET /products/{id}/rating-summaryusing an aggregation ($match,$groupaverage and count). - Run it against 100 000 seeded reviews; compare timings with and without the index.
- Planned mistake: embed the review list inside a
ProductReviewsdocument; at a few thousand reviews updates slow down. Fix: one document per review and a separate summary. - Use Testcontainers with a MongoDB image for the integration test (verify image tag).
Check your understanding
Where is the average computed?
Show answer
In the database, by the aggregation pipeline.
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.