Chapter 2 · Practice 4 of 5
Avoid bloated documents and collection sprawl
From Schema design
Why it matters
Two more anti-patterns from the MongoDB docs:
Bloated documents. When a document carries large fields that most queries do not need (a long description, a base64 image, a full audit trail), every read pulls them into memory. Move rarely used large fields to a separate collection, and store files in object storage or GridFS, not inline.
Too many collections. Creating a collection per customer, per day or per tenant feels tidy, but the docs note that a large number of collections decreases storage engine performance. Use one collection with a field (tenantId, day) and an index on it instead.
The docs also list excessive $lookup as an anti-pattern: if a common query needs several lookups, that is a sign the data should be shaped differently.
Avoid
- A collection per customer or per day
- Images or files stored inline in documents
- Several
$lookupstages on a hot query path