Subset Pattern

noun

say it: “SUB-set PAT-ern” /ˈsʌbsɛt ˈpætərn/

A data design that keeps only the most-used few items of a long list inside a record, and the full list elsewhere.

What is the subset pattern?

The subset pattern keeps only the few items a page shows, such as a product's five newest reviews, inside the document, and the whole list in a collection of its own. The page reads one small document, and the full list is there when someone asks for it.

db.reviews.insertOne({ productId: 7, stars: 5, text: "Fits well" });   // the whole list
db.products.updateOne({ _id: 7 }, {
  $push: { recentReviews: { $each: [{ stars: 5, text: "Fits well" }], $position: 0, $slice: 5 } }
});   // only the 5 newest stay inside the product

In a sentence

  • “The subset pattern kept the product document small even after 3,000 reviews.”
  • “With the subset pattern, the product page shows five reviews from the product document and loads the rest only on request.”

Level: Journeyman · More MongoDB terms at this level

Updated