MongoDB Data Modeling

Introduction to Data Modeling

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.

MongoDB Data Modeling

Data Modeling Patterns in MongoDB

Key Considerations

Application Requirements

  • Query patterns and access patterns
  • Data relationships and cardinality
  • Data growth and scalability
  • Performance requirements

Data Characteristics

  • Data size and growth rate
  • Data volatility (read/write ratio)
  • Data consistency requirements
  • Data access patterns

Data Modeling Patterns

Embedded Document Pattern

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"]
    }
}

Referenced Document Pattern

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"
}

Subset Pattern

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
}

Extended Reference Pattern

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
    }]
}

Best Practices

Design Guidelines

  • Design for your application's access patterns
  • Consider data growth and scalability
  • Balance between embedding and referencing
  • Optimize for read vs. write operations
  • Use appropriate data types
  • Implement proper indexing strategies

Common Pitfalls to Avoid

  • Over-embedding large arrays
  • Creating too many collections
  • Ignoring data access patterns
  • Not considering data growth
  • Poor indexing strategy

Real-World Examples

E-commerce Application

// 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!"
    }]
}

Social Media Application

// 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")
        }]
    }]
}

Next Steps

Now that you understand data modeling, you can explore: