NoSQL Modeling 101
Data modeling in MongoDB is fundamentally different from SQL. The golden rule: Data that is accessed together should be stored together.
Embedding vs Referencing
This is the most critical decision in MongoDB schema design.
Embedding (Denormalization)
Use when you have "contains" relationships or one-to-few.
// User Document with Embedded Address
{
"_id": "user123",
"name": "Jane Doe",
"addresses": [
{ "street": "123 Main St", "city": "New York" }
]
}
Pros: Fast reads (single query). Cons: Potential data duplication.
Referencing (Normalization)
Use when you have one-to-many (unbounded) or many-to-many relationships.
// User Document
{ "_id": "user123", "name": "Jane Doe" }
// Order Document
{ "_id": "order999", "user_id": "user123", "total": 50 }
Indexing Strategies
Indexes support the efficient execution of queries. Without indexes, MongoDB must perform a collection scan.
- Single Field Index: Basic indexing.
- Compound Index: Index on multiple fields (order matters!).
- Multikey Index: Identify data in arrays.