Designing schemas for NoSQL requires thinking about access patterns rather than relational entities. Data that is queried together should be stored together.
1. The Core Decision Framework
┌─────────────────────────────────┬─────────────────────────────────┐
│ Embed When: │ Reference When: │
├─────────────────────────────────┼─────────────────────────────────┤
│ Relationships are "One-to-Few" │ Relationships are "One-to-Many" │
│ Subdocuments rarely change │ Subdocuments updated frequently │
│ Data is always read together │ Data is queried independently │
│ Document stays under 16MB limit │ Embedded array would grow huge │
└─────────────────────────────────┴─────────────────────────────────┘
2. Essential NoSQL Design Patterns
1. The Subset Pattern (High-Traffic Detail Views)
When a document contains large arrays (e.g. an e-commerce product with 50,000 customer reviews), embedding all reviews will exceed the 16MB document limit and cause massive network transfer latency on catalog views.
Solution: Embed only the top 10 most recent reviews inside the main products document, and store the remaining historical reviews in a separate reviews collection referenced by product_id:
// products collection (Fast 2KB document read for product pages!)
{
"_id": ObjectId("655f1a9b2c8d4e0012a4b8c1"),
"name": "Wireless Noise-Canceling Headphones",
"price": 299.99,
"recent_reviews": [
{ "user": "Alex", "rating": 5, "comment": "Outstanding acoustic fidelity." },
{ "user": "Elena", "rating": 4, "comment": "Comfortable for long flights." }
],
"total_reviews": 4820
}
2. The Computed Pattern (Pre-Aggregating Metrics)
Calculating sums, averages, or counts across millions of rows at read time degrades database CPU.
Solution: Pre-calculate aggregates during write operations:
// When a new sale is made, increment the pre-computed totals atomically:
await db.authors.updateOne(
{ _id: authorId },
{
$inc: {
"metrics.total_books_sold": 1,
"metrics.total_revenue": bookPrice
},
$set: { "metrics.last_sale_at": new Date() }
}
);
3. The Bucket Pattern (Time-Series & IoT Telemetry)
Instead of storing 1,000,000 separate sensor reading documents, group readings into hourly or daily buckets:
{
"_id": ObjectId("..."),
"sensor_id": "SENSOR-ALPHA-01",
"bucket_date": ISODate("2026-11-21T14:00:00Z"),
"sample_count": 3600,
"temperature_readings": [
{ "sec": 0, "val": 21.4 },
{ "sec": 1, "val": 21.5 },
{ "sec": 2, "val": 21.6 }
]
}
4. The Schema Versioning Pattern (Zero-Downtime Migrations)
In relational databases, schema migrations require alter table locks. In NoSQL, documents can carry a schema_version field:
// Schema version 1
{ "_id": 1, "name": "Jane Doe", "schema_version": 1 }
// Schema version 2 (split into first and last name)
{ "_id": 2, "first_name": "John", "last_name": "Smith", "schema_version": 2 }
Your application code checks doc.schema_version and upgrades documents on-the-fly during read operations with zero database downtime!