All projects
E-commerce Store
Product catalog, carts, orders, and reviews — the canonical MongoDB starter project.
Schema design
products
Catalog items. Variants are embedded because they are always displayed with the product.
products document
{
_id: ObjectId(),
name: "Trail Running Shoe",
slug: "trail-running-shoe",
price: 89.99,
category: "footwear",
attributes: { brand: "Apex", weightG: 280 },
variants: [
{ sku: "TRS-42-BLK", size: 42, color: "black", stock: 12 },
{ sku: "TRS-43-BLK", size: 43, color: "black", stock: 5 }
],
avgRating: 4.6, // computed pattern: updated on review write
reviewCount: 128
}orders
Extended-reference pattern: line items copy name & price at purchase time so history never changes when the catalog does.
orders document
{
_id: ObjectId(),
userId: ObjectId(),
status: "paid", // cart | paid | shipped | delivered | cancelled
items: [
{ productId: ObjectId(), name: "Trail Running Shoe",
sku: "TRS-42-BLK", unitPrice: 89.99, qty: 1 }
],
total: 89.99,
shippingAddress: { line1: "...", city: "...", zip: "..." },
placedAt: ISODate()
}reviews
Referenced (not embedded in products) because reviews are unbounded and queried independently.
reviews document
{
_id: ObjectId(),
productId: ObjectId(),
userId: ObjectId(),
rating: 5,
title: "Great grip",
body: "...",
createdAt: ISODate()
}Key queries
Product page by slug
mongosh
db.products.findOne({ slug: "trail-running-shoe" })In-stock variants of a size
mongosh
db.products.find({
variants: { $elemMatch: { size: 42, stock: { $gt: 0 } } }
})A user's order history (newest first)
mongosh
db.orders.find({ userId: userId })
.sort({ placedAt: -1 }).limit(10)Indexes
db.products.createIndex({ slug: 1 }, { unique: true })Product pages look up by slug — must be unique and instant.
db.products.createIndex({ category: 1, price: 1 })Category browsing with price sorting/filtering (ESR: equality then range).
db.orders.createIndex({ userId: 1, placedAt: -1 })Order history is always per-user, newest first — the index matches exactly.
db.reviews.createIndex({ productId: 1, createdAt: -1 })Reviews are read per-product, newest first.
Aggregations
Revenue per month
mongosh
db.orders.aggregate([
{ $match: { status: { $in: ["paid", "shipped", "delivered"] } } },
{ $group: {
_id: { $substrCP: ["$placedAt", 0, 7] }, // "2025-07"
revenue: { $sum: "$total" },
orders: { $sum: 1 }
}},
{ $sort: { _id: 1 } }
])Best sellers
mongosh
db.orders.aggregate([
{ $unwind: "$items" },
{ $group: { _id: "$items.productId",
sold: { $sum: "$items.qty" } } },
{ $sort: { sold: -1 } }, { $limit: 10 }
])Performance tips
- 1.Update avgRating/reviewCount on the product when a review is written (computed pattern) instead of aggregating reviews on every product page view.
- 2.Copy price & name into order line items — orders are immutable history.
- 3.Keep carts in the orders collection with status 'cart' and a TTL index on updatedAt to auto-expire abandoned carts.
Want to experiment with these patterns? Open the playground and build the collections yourself, or practice related skills in the challenges.