Model for how the app reads data: embed one-to-few, reference one-to-many, bucket high-cardinality time series.
Embed one-to-few
db.orders.insertOne({
customerId: 'c-1',
lines: [
{ sku: 'A1', qty: 1, price: 9.99 },
{ sku: 'B2', qty: 2, price: 12.0 }
]
})Practice: Run on practice in mongosh.
Reference one-to-many
// orders hold customerId; customers in separate collection
db.customers.insertOne({ _id: 'c-1', name: 'Acme' })
db.orders.insertOne({ customerId: 'c-1', total: 50 })
Bucket pattern (concept)
Store time-series events in documents with arrays capped per hour/day to avoid unbounded document growth.
Important interview questions and answers
- Q: Embedding downside?
A: Document growth and duplicate data when parent copies change. - Q: Reference downside?
A: Requires second query or $lookup—more round trips.
Self-check
- When embed line items?
- When reference customers instead?
Tip: Embed one-to-few; reference one-to-many unbounded children.
Interview prep
- Embed when?
- One-to-few, read together, bounded size.
- Bucket pattern?
- Groups time-series events to cap document growth.