Keeping Documents Small: the Subset and Computed Patterns

A product page shows its five newest reviews from a short copy kept in the product (the subset pattern), and its average rating from a count and a total that each new review adds to with $inc (the computed pattern), so the page never reads or recounts every review.

Journeyman · MongoDB · Updated

By

After this lesson you can

  • Keep the newest items of a growing list in a document with $push, $each, $position and $slice, the subset pattern
  • Keep a count and a total up to date with $inc on each write, the computed pattern, and work out an average from them

Key takeaways

  • The subset pattern keeps every item of a growing list in its own collection, and copies only the few a page shows into the document.
  • $push with $each, $position 0 and $slice 5 puts a new item at the front of an array and keeps only the first five.
  • The computed pattern works a number out once, when the data is written, instead of on every read.
  • An average can't be added to, but the count and the total it comes from can, with $inc. The page divides one by the other.
  • Both patterns change the product in the same updateOne that adds the review, so its copies never disagree.

A product page with "what customers say", a profile with "recent activity", a post with its top comments: each shows a few items from a list that keeps growing, and often one number that sums up the whole list, such as 4.5 stars from 12 reviews. You'll use these two patterns whenever a document would otherwise carry a list that never stops growing, or a page would add up the same numbers on every visit.

The examples use Riverbend's catalog from Copying a Few Fields: the Extended Reference Pattern. In mongosh, after use riverbend, load it again first:

load("riverbend-catalog.js")
true

Every review, or none?

The Riverbend Road 2 is the shop's best seller, and its page shows what riders say about it. There are two plain ways to store its reviews, and both break as the reviews pile up:

  • Embed every review in the product. One read gets them all, but the product grows with every review, forever. A best seller with 3,000 reviews makes every page view read 3,000 reviews to show five, and an array that never stops growing heads for the 16 MB limit from Embedding: Keeping Related Data Together.
  • Keep every review in its own collection. The product stays small, but every page view asks a second collection, then counts and averages every review again.

Riverbend keeps every review in the reviews collection, where a "see all reviews" page can find them:

db.reviews.countDocuments({ productId: 101 })
12

The subset pattern: the newest five come along

The page shows only the five newest reviews, so the product keeps a copy of those five, newest first, and nothing more:

db.products.findOne({ _id: 101 }, { name: 1, "reviews.by": 1, "reviews.stars": 1 })
{
  _id: 101,
  name: 'Riverbend Road 2',
  reviews: [
    { by: 'Ana', stars: 5 },
    { by: 'Omar', stars: 4 },
    { by: 'Grace', stars: 5 },
    { by: 'Dev', stars: 5 },
    { by: 'Lena', stars: 4 }
  ]
}

Keeping the few items a page shows in the document, and the whole list somewhere else, is the subset pattern. The page needs one read; the full list is there for the rare visitor who clicks "see all".

Adding a review: $each, $position and $slice

Tess leaves a review. It goes into reviews in full:

db.reviews.insertOne({ _id: 313, productId: 101, by: "Tess", stars: 4, text: "Shifts like butter.", date: ISODate("2026-11-14") })
{ acknowledged: true, insertedId: 313 }

Then the product's copy has to change: Tess at the front, and the oldest of the five gone. $push from More Update Operators: $inc, $push, $pull and $addToSet adds to the end and never removes, so it takes three helpers here:

db.products.updateOne({ _id: 101 }, {
  $push: { reviews: {
    $each: [ { by: "Tess", stars: 4, text: "Shifts like butter.", date: ISODate("2026-11-14") } ],
    $position: 0,
    $slice: 5
  } },
  $inc: { reviewCount: 1, starsTotal: 4 }
})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

Read the $push part aloud: "push each of these reviews ($each, a list in square brackets, even for one), at position 0, the front ($position), then keep only the first five ($slice, read "slice")". The $inc line is the second pattern, coming up next.

db.products.findOne({ _id: 101 }, { "reviews.by": 1, "reviews.stars": 1 })
{
  _id: 101,
  reviews: [
    { by: 'Tess', stars: 4 },
    { by: 'Ana', stars: 5 },
    { by: 'Omar', stars: 4 },
    { by: 'Grace', stars: 5 },
    { by: 'Dev', stars: 5 }
  ]
}

Tess is first, and Lena, the oldest of the five, has dropped off the product. Her review is still in reviews, so nothing is lost.

The Riverbend Road 2's product card holds five review slips, newest first: Tess, Ana, Omar, Grace and Dev. Lena's slip, the oldest, falls off the end into a large box labeled reviews collection, which holds every review.

The computed pattern: count once, when it's written

The page also shows the average rating. Adding up 13 reviews on every page view is wasted work, and with 3,000 reviews it's slow. The computed pattern works the number out once, when a review is written, and stores it in the product.

There's a catch: you can't add a new review to an average. If the average is 4.5, a new 4 doesn't tell you the new average unless you know how many reviews made the 4.5. So the product keeps the two numbers an average comes from, and $inc adds to both. That was the $inc line in the update above: one more review, four more stars.

db.products.findOne({ _id: 101 }, { name: 1, reviewCount: 1, starsTotal: 1 })
{ _id: 101, name: 'Riverbend Road 2', reviewCount: 13, starsTotal: 58 }

The page divides: 58 stars from 13 reviews is about 4.46, shown as 4.5 stars. The push and the $inc sit in the same updateOne, so the five reviews, the count and the total always change together. If they ever had to be checked, every review is still in reviews to count again.

Pattern What the product keeps Changed by
Subset The five newest reviews $push with $each, $position: 0 and $slice: 5
Computed reviewCount and starsTotal $inc, in the same update

Check yourself

2 questions to try on the lesson page.

So what

A best seller's page stays one small, fast read however many reviews it collects: the newest five come along, the numbers are already added up, and every review is still kept.

Key terms

Related