MongoDB Schema Design Interview Questions

Introduction

This guide covers essential MongoDB schema design concepts commonly asked in technical interviews. Each question includes detailed answers and practical examples.

Medium

1. What are the key considerations in MongoDB schema design?

Key considerations in MongoDB schema design include:

  • Query patterns and access patterns
  • Data relationships and cardinality
  • Data growth and scalability
  • Performance requirements
  • Data consistency needs
Design Principles:
  • Design for query patterns
  • Consider data relationships
  • Plan for scalability
  • Balance normalization and denormalization
Medium

2. Explain common MongoDB schema patterns

Common MongoDB schema patterns include:

1. Embedded Document Pattern
// User with embedded address
{
    _id: ObjectId("..."),
    name: "John Doe",
    email: "john@example.com",
    address: {
        street: "123 Main St",
        city: "New York",
        state: "NY",
        zip: "10001"
    }
}
2. Array of Subdocuments Pattern
// Order with embedded items
{
    _id: ObjectId("..."),
    orderNumber: "ORD123",
    customerId: "CUST456",
    items: [
        {
            productId: "PROD1",
            name: "Laptop",
            quantity: 1,
            price: 999.99
        },
        {
            productId: "PROD2",
            name: "Mouse",
            quantity: 2,
            price: 29.99
        }
    ]
}
Hard

3. How do you handle relationships in MongoDB?

MongoDB supports several relationship patterns:

  • One-to-One relationships
  • One-to-Many relationships
  • Many-to-Many relationships
// One-to-One: Embedded Document
{
    _id: ObjectId("..."),
    name: "John Doe",
    passport: {
        number: "P123456",
        expiryDate: ISODate("2025-12-31")
    }
}

// One-to-Many: Reference Pattern
// Parent document
{
    _id: ObjectId("..."),
    name: "Department A",
    employees: [
        ObjectId("emp1"),
        ObjectId("emp2")
    ]
}

// Child documents
{
    _id: ObjectId("emp1"),
    name: "Employee 1",
    departmentId: ObjectId("...")
}

// Many-to-Many: Array of References
{
    _id: ObjectId("..."),
    name: "Project X",
    teamMembers: [
        ObjectId("emp1"),
        ObjectId("emp2"),
        ObjectId("emp3")
    ]
}
Hard

4. What are the best practices for schema validation?

Schema validation best practices include:

  • Using JSON Schema validation
  • Defining required fields
  • Setting data types and constraints
  • Implementing custom validation
// Create collection with validation
db.createCollection("users", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            required: ["name", "email", "age"],
            properties: {
                name: {
                    bsonType: "string",
                    minLength: 2
                },
                email: {
                    bsonType: "string",
                    pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
                },
                age: {
                    bsonType: "int",
                    minimum: 18,
                    maximum: 120
                },
                address: {
                    bsonType: "object",
                    properties: {
                        street: { bsonType: "string" },
                        city: { bsonType: "string" },
                        zip: { bsonType: "string" }
                    }
                }
            }
        }
    },
    validationLevel: "strict",
    validationAction: "error"
})
Medium

5. How do you handle schema evolution?

Schema evolution strategies include:

  • Versioning documents
  • Using flexible schemas
  • Implementing migration scripts
  • Handling backward compatibility
// Versioning documents
{
    _id: ObjectId("..."),
    schemaVersion: 2,
    name: "John Doe",
    email: "john@example.com",
    // New fields in version 2
    phone: "+1234567890",
    preferences: {
        notifications: true,
        theme: "dark"
    }
}

// Migration script example
db.users.find({ schemaVersion: 1 }).forEach(function(doc) {
    db.users.update(
        { _id: doc._id },
        {
            $set: {
                schemaVersion: 2,
                phone: "",
                preferences: {
                    notifications: true,
                    theme: "light"
                }
            }
        }
    );
});
Hard

6. How do you optimize schema for performance?

Schema optimization techniques include:

  • Denormalization for read performance
  • Indexing strategy
  • Document size optimization
  • Query pattern alignment
// Denormalized schema for read performance
{
    _id: ObjectId("..."),
    orderNumber: "ORD123",
    customer: {
        name: "John Doe",
        email: "john@example.com",
        address: "123 Main St"
    },
    items: [
        {
            productId: "PROD1",
            name: "Laptop",
            price: 999.99,
            category: "Electronics"
        }
    ],
    totalAmount: 999.99,
    status: "completed",
    createdAt: ISODate("2025-01-01")
}

// Create appropriate indexes
db.orders.createIndex({ orderNumber: 1 })
db.orders.createIndex({ "customer.email": 1 })
db.orders.createIndex({ createdAt: -1 })
db.orders.createIndex({ status: 1, createdAt: -1 })

Next Steps

Continue your MongoDB interview preparation with: