This guide covers essential MongoDB document-oriented database concepts commonly asked in technical interviews. Each question includes detailed answers and practical examples.
A document-oriented database is a type of NoSQL database that stores data in flexible, JSON-like documents. Key characteristics include:
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"]
}
]
}
Key best practices for MongoDB document design include:
// 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")
]
}
MongoDB supports several approaches for handling relationships:
// 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")
]
}
MongoDB has a 16MB document size limit. To handle this limitation:
// 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")
}
Schema evolution in MongoDB can be handled through:
// 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
}
}
);
});
Continue your MongoDB interview preparation with: