Modeling Patterns
Modeling Patterns: Copying a Few Fields: the Extended Reference Pattern, Keeping Documents Small: the Subset and Computed Patterns, Different Shapes in One…
Updated
- Copying a Few Fields: the Extended Reference Pattern (Journeyman): An order can keep a product's _id together with a copy of its name and the price paid, so a "my orders" page needs one read and the price stays what the customer paid; copy only a few fields, and decide for each one whether it stays as history or follows the original.
- Keeping Documents Small: the Subset and Computed Patterns (Journeyman): 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.
- Different Shapes in One Collection: the Polymorphic Pattern (Journeyman): Bikes, helmets and lights can share one products collection, each with the fields every product has, its own fields, and a type field that says which kind it is, so one query can search them all and another can pick out one kind.