The Extended Reference and Subset Patterns
Learners will embed a curated subset of frequently accessed fields from a referenced document to eliminate $lookup joins on hot read paths.
The $lookup Performance Problem
MongoDB's $lookup stage joins collections at query time by performing an in-memory hash join. For frequent, high-traffic queries, this join cost can dominate latency. If your order list endpoint performs a $lookup to fetch customer names and emails for every order in every request, you pay the join cost every single time. The Extended Reference Pattern eliminates this cost by embedding a targeted subset of the referenced document's fields.
The Extended Reference Pattern
The Extended Reference Pattern embeds a carefully selected subset of fields from a referenced document directly inside the referencing document. Instead of storing only a customer _id in an order and looking up the customer for every order display, embed the customer's name and email — the fields needed to render the order — directly in the order document. The full customer record still exists separately for updates.
// Without Extended Reference: requires $lookup on every order read
{ orderId: 'ORD-1234', customerId: ObjectId('...'), total: 99.99 }
// With Extended Reference: order carries the fields it needs
{
orderId: 'ORD-1234',
customer: {
_id: ObjectId('...'), // reference for updates
name: 'Alice Smith', // duplicated for fast reads
email: 'alice@example.com' // duplicated for fast reads
},
total: 99.99,
status: 'shipped'
}All lessons in this course
- The Bucket and Computed Patterns
- The Extended Reference and Subset Patterns
- The Polymorphic and Schema Versioning Patterns
- The Outlier and Tree Structure Patterns