Data modeling in MongoDB is the process of designing the structure of your documents and collections to support your application's requirements. Unlike relational databases, MongoDB's flexible schema allows for more dynamic data structures.
Data Modeling Patterns in MongoDB
Store related data within a single document for efficient reads.
// Example: User with embedded profile
{
_id: "user123",
name: "John Doe",
email: "john@example.com",
profile: {
bio: "Software Developer",
location: "New York",
interests: ["coding", "reading"]
}
}
Use references to link related documents across collections.
// Example: User with referenced posts
{
_id: "user123",
name: "John Doe",
posts: ["post1", "post2", "post3"]
}
// Posts collection
{
_id: "post1",
title: "MongoDB Tutorial",
content: "...",
author: "user123"
}
Store frequently accessed data in a separate collection.
// Main product document
{
_id: "product123",
name: "Laptop",
description: "Detailed description...",
specifications: {...},
reviews: [...]
}
// Subset for quick access
{
_id: "product123",
name: "Laptop",
price: 999.99,
rating: 4.5
}
Include frequently accessed fields from referenced documents.
// Order with extended product reference
{
_id: "order123",
user: "user123",
products: [{
product_id: "product123",
name: "Laptop",
price: 999.99,
quantity: 1
}]
}
// Product document
{
_id: "product123",
name: "Smartphone",
price: 699.99,
category: "Electronics",
inventory: {
in_stock: 100,
reserved: 5
},
specifications: {
brand: "TechBrand",
model: "X1",
color: "Black"
},
reviews: [{
user_id: "user456",
rating: 5,
comment: "Great product!"
}]
}
// User document
{
_id: "user123",
username: "johndoe",
profile: {
name: "John Doe",
bio: "Software Developer",
location: "New York"
},
followers: ["user456", "user789"],
following: ["user456"],
posts: [{
_id: "post1",
content: "Hello World!",
timestamp: ISODate("2025-01-01"),
likes: 10,
comments: [{
user_id: "user456",
text: "Great post!",
timestamp: ISODate("2025-01-01")
}]
}]
}
Now that you understand data modeling, you can explore: