Implementing Data Modeling Patterns
1. Using Attribute Pattern
| Aspect | Detail |
|---|---|
| Use | Many similar fields differing per row |
| Model | Array of {key, value} pairs |
| Benefit | Index one path instead of many columns |
| MongoDB example | attrs: [{k:"color",v:"red"},{k:"size","M"}] |
2. Using Extended Reference Pattern
| Aspect | Detail |
|---|---|
| Use | Embed frequently-accessed fields from referenced entity |
| Trade-off | Read perf vs sync complexity |
| Example | order.customer_name + customer_id |
3. Using Subset Pattern
| Aspect | Detail |
|---|---|
| Use | Document grows unbounded (large arrays) |
| Solution | Keep recent N inline; rest in separate collection |
| Benefit | Limit doc size, faster reads of common slice |
| Example | movie.recent_reviews (10) + reviews collection |
4. Using Computed Pattern
| Aspect | Detail |
|---|---|
| Use | Read-heavy derived value (e.g., averages, totals) |
| Solution | Pre-compute & store; refresh on write or periodically |
| Trade-off | Read perf vs write cost / staleness |
5. Using Bucket Pattern
| Aspect | Detail |
|---|---|
| Use | High-frequency time-series writes |
| Solution | Group N points per parent doc (bucket) |
| Benefit | Fewer documents, better compression |
| Example | IoT readings — 1 doc per hour with array of samples |
6. Using Outlier Pattern
| Aspect | Detail |
|---|---|
| Use | Few documents dwarf the rest |
| Solution | Flag outlier, move overflow data to separate doc |
| Example | Most books have < 100 reviews; bestsellers have 100k |
7. Using Schema Versioning Pattern
| Aspect | Detail |
|---|---|
| Use | Schema-less DB with evolving structure |
| Solution | Embed schema_version field |
| Migration | Lazy: upgrade on read; or batch background |
8. Using Polymorphic Pattern
| Aspect | Detail |
|---|---|
| Use | Similar entities with varying fields in one collection |
| Solution | type field discriminator + type-specific fields |
| Example | vehicles: cars + trucks + motorcycles with shared base |
9. Using Approximation Pattern
| Aspect | Detail |
|---|---|
| Use | Exact count not needed (page views, likes) |
| Solution | Increment counter only every Nth event |
| Benefit | Reduces write load N× |
| Alternatives | HyperLogLog for cardinality, Bloom filter for membership |
10. Using Tree Pattern
| Model | Use |
|---|---|
| Parent reference | Simplest; recursive lookup |
| Child references (array) | Fast children query |
| Array of ancestors | Fast ancestor query |
| Materialized path | String-based path traversal |
| Nested set | Read-heavy, complex trees |