Skip to content
Ameen Abozaid
MID-042

MongoDB

  • 7 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Document modeling
  • indexing
  • aggregation
  • transactions
  • use cases

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Model documents with embedding vs referencing decided by access patterns.
  2. Create indexes and read query plans in MongoDB.
  3. Write aggregation pipelines.
  4. Use multi-document transactions where needed and explain their cost.
  5. Use MongoDB from Spring Data MongoDB.

Lessons

  1. 01Documents and collections
  2. 02Embedding vs referencing
  3. 03Indexes and explain
  4. 04Aggregation pipelines
  5. 05Transactions
  6. 06Spring Data MongoDB
  7. 07Demo: building the reviews service on MongoDB

Lab and exercises

Lab

MID-042-LAB1 · Reviews service

Outcomes: 1–5 · Time: ~2.5 h Build a reviews service on MongoDB with Spring Data MongoDB: add a review, list the latest 20 for a product, and return a ratings summary (average and count per star) computed by an aggregation. Create indexes for the queries and show their plans with explain. Acceptance: indexes match queries; summary computed in the database.

Exercises (assessment: document-model review)

IDOutcomeTask
MID-042-EX011Decide embed or reference: order items, product reviews, customer address in an order, product category.
MID-042-EX022Create the right index for "latest reviews of a product" and show the plan before and after.
MID-042-EX033Write a pipeline that returns the count per rating for product 42.
MID-042-EX044Give one case that needs a multi-document transaction and one that can be modeled away; state the cost.
MID-042-EX055Write a MongoRepository with a derived query for the 20 latest reviews of a product.
MID-042-EX061Review a given document model of Product with an embedded reviews array and a table-per-entity layout; list the problems and a fix.
Show solutions
  • EX01 Order items: embed (owned, bounded). Reviews: reference. Address in order: embed a snapshot. Category: reference id and copy the name.
  • EX02 createIndex({productId:1, createdAt:-1}); plan changes from COLLSCAN to IXSCAN with totalDocsExamined close to nReturned.
  • EX03 [{ $match:{productId:42} }, { $group:{ _id:"$rating", count:{$sum:1} } }, { $sort:{_id:-1} }].
  • EX04 Needed: move credit between two wallet documents. Modeled away: update an order and its embedded items in one document. Cost: extra latency, retries, locking, replica set required.
  • EX05 List<Review> findTop20ByProductIdOrderByCreatedAtDesc(long productId);
  • EX06 Unbounded array (growth, 16 MB, slow updates) and relational copying (many lookups). Fix: separate reviews collection plus an embedded small summary; embed data that is read together.

Outcome 5 is also covered by the lab.

Quiz

Outcomes: O1 embedding vs referencing · O2 indexes and explain · O3 aggregation · O4 transactions · O5 Spring Data MongoDB.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Embed data when it is:O1
  2. 2The classic MongoDB modeling mistake:O1
  3. 3COLLSCAN in explain means:O2
  4. 4For find({productId}).sort({createdAt:-1}) the best index is:O2
  5. 5In explain("executionStats"), a healthy query has totalDocsExamined that is:O2
  6. 6An aggregation pipeline should start with:O3
  7. 7To compute an average rating per product use:O3
  8. 8A single-document update is:O4
  9. 9A cost of multi-document transactions:O4
  10. 10In Spring Data MongoDB a class is mapped with:O5

Practical part

Document-model review (MID-042-EX06) plus lab MID-042-LAB1. Pass: quiz at least 8/10, a review that names both problems, and a lab with matching indexes and a database-computed summary.

Common mistakes

Unbounded arrays inside documents; relational schemas copied into collections.

Assessment

Document-model review.

Career relevance

Polyglot persistence decisions.

Type to search levels, phases, modules and lessons.