MongoDB Best Practices

Chapter 2 · Practice 2 of 5

Embed what you read together; reference what grows or is shared

From Schema design

Why it matters

Both embedding and referencing are valid. Choose per relationship:

Embed whenReference when
The child is always read with the parentThe child is often read on its own
The set of children is small and boundedThe set can grow without limit
The child belongs to one parentMany parents share the child
You want atomic updates of bothThe child changes often and independently

The MongoDB docs give a useful hybrid: keep the five most recent reviews embedded in the product document for the product page, and store all reviews in a separate collection. The common read is one document; the full history stays unbounded where it belongs.

Example

JavaScript
// products: the page needs recent reviews in one read
{
  _id: "sku_a1",
  name: "Desk lamp",
  price: NumberDecimal("39.00"),
  recentReviews: [
    { userId: "u_12", rating: 5, text: "Bright and sturdy", at: ISODate("2026-10-02") }
  ]
}

// reviews: the full, growing history
{ _id: ObjectId(), productId: "sku_a1", userId: "u_12", rating: 5, text: "...", at: ISODate() }