MongoDB Document-Oriented Database Interview Questions

Introduction

This guide covers essential MongoDB document-oriented database concepts commonly asked in technical interviews. Each question includes detailed answers and practical examples.

Easy

1. What is a Document-Oriented Database?

A document-oriented database is a type of NoSQL database that stores data in flexible, JSON-like documents. Key characteristics include:

  • Schema-less design
  • Hierarchical data structure
  • Self-contained documents
  • Rich query capabilities
Advantages:
  • Flexible schema evolution
  • Natural data representation
  • Efficient querying
  • Scalable architecture
Medium

2. How do you structure documents in MongoDB?

MongoDB documents can be structured in various ways depending on your application needs:

// Flat Document Structure
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "age": 30,
    "email": "john@example.com",
    "phone": "123-456-7890"
}

// Embedded Document Structure
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "contact": {
        "email": "john@example.com",
        "phone": "123-456-7890",
        "address": {
            "street": "123 Main St",
            "city": "New York",
            "zip": "10001"
        }
    }
}

// Array of Documents
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "orders": [
        {
            "orderId": "ORD001",
            "date": ISODate("2023-01-01"),
            "items": ["item1", "item2"]
        },
        {
            "orderId": "ORD002",
            "date": ISODate("2023-01-15"),
            "items": ["item3"]
        }
    ]
}
Medium

3. What are the best practices for document design?

Key best practices for MongoDB document design include:

  • Embed vs. Reference: Choose based on data relationships and access patterns
  • Document Size: Keep documents under 16MB
  • Atomicity: Design for atomic operations
  • Indexing: Plan indexes based on query patterns
// Good Practice: Embedded Documents for One-to-One
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "user": {
        "name": "John Doe",
        "email": "john@example.com"
    },
    "profile": {
        "bio": "Software Developer",
        "location": "New York"
    }
}

// Good Practice: References for One-to-Many
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "orderIds": [
        ObjectId("507f1f77bcf86cd799439012"),
        ObjectId("507f1f77bcf86cd799439013")
    ]
}
Hard

4. How do you handle relationships in document databases?

MongoDB supports several approaches for handling relationships:

  • Embedded Documents: For one-to-one and one-to-few relationships
  • References: For one-to-many and many-to-many relationships
  • Hybrid Approach: Combining both based on access patterns
// One-to-One: Embedded
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "address": {
        "street": "123 Main St",
        "city": "New York"
    }
}

// One-to-Many: References
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "posts": [
        ObjectId("507f1f77bcf86cd799439012"),
        ObjectId("507f1f77bcf86cd799439013")
    ]
}

// Many-to-Many: References
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "roles": [
        ObjectId("507f1f77bcf86cd799439014"),
        ObjectId("507f1f77bcf86cd799439015")
    ]
}
Medium

5. What are the limitations of document size in MongoDB?

MongoDB has a 16MB document size limit. To handle this limitation:

  • Use GridFS for large files
  • Split data into multiple documents
  • Use references for large related data
  • Optimize document structure
// Using GridFS for large files
const { GridFSBucket } = require('mongodb');
const bucket = new GridFSBucket(db);

// Upload large file
bucket.uploadFromStream('large-file.pdf', fileStream);

// Using references for large data
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "John Doe",
    "largeDataId": ObjectId("507f1f77bcf86cd799439016")
}
Hard

6. How do you handle schema evolution in MongoDB?

Schema evolution in MongoDB can be handled through:

  • Versioning documents
  • Using default values
  • Migration scripts
  • Application-level validation
// Versioning Documents
{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "schemaVersion": 2,
    "name": "John Doe",
    "email": "john@example.com",
    "phone": "123-456-7890"  // New field in version 2
}

// Using Default Values
db.users.updateMany(
    { phone: { $exists: false } },
    { $set: { phone: "N/A" } }
)

// Migration Script
db.users.find({ schemaVersion: 1 }).forEach(function(doc) {
    db.users.update(
        { _id: doc._id },
        {
            $set: {
                phone: "N/A",
                schemaVersion: 2
            }
        }
    );
});

Next Steps

Continue your MongoDB interview preparation with: