MongoDB Fundamentals and Core Concepts
1. What is MongoDB, and what are its key characteristics?
MongoDB is a source-available, cross-platform, document-oriented database program. Classified as a NoSQL database, it stores data in flexible, JSON-like documents (BSON format), enabling dynamic schemas. Key characteristics include scalability (horizontal via sharding), high availability (via replica sets), flexibility (schemaless), and powerful querying (rich query language, aggregation framework). It prioritizes developer agility and speed of development over strict ACID compliance in all scenarios.
// Example of a MongoDB document
{
"_id": ObjectId("65e6d0a7a4b0c1d2e3f4a5b6"),
"name": "Alice Wonderland",
"email": "alice@example.com",
"preferences": ["email_notifications", "sms_alerts"],
"address": {
"street": "123 Rabbit Hole",
"city": "Wonderland",
"zip": "90210"
},
"last_login": ISODate("2024-03-05T10:00:00Z")
}
2. Explain the concept of a "document" in MongoDB.
A document is MongoDB's fundamental unit of data, akin to a row in a relational database but far more flexible. It's an ordered set of key-value pairs stored in BSON (Binary JSON) format. Documents can contain nested documents, arrays, and various data types. This structure allows for dynamic schemas, meaning documents in the same collection don't need to have identical fields, facilitating rapid evolution of data models. Each document typically has a unique _id field.
3. What is BSON, and why does MongoDB use it instead of JSON?
BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents. MongoDB uses BSON because it offers several advantages over plain JSON: efficiency (faster to parse and traverse), more data types (supports types like ObjectId, Date, BinData, Timestamp, which JSON lacks), and embeddability (documents can contain other documents and arrays easily). While conceptually similar to JSON, BSON provides a richer and more performant data representation for database operations.
4. Differentiate between _id and ObjectId.
The _id field is the primary key for every document in a MongoDB collection, ensuring its uniqueness within that collection. If not explicitly provided, MongoDB automatically generates an _id. An ObjectId is a common type used for the _id field. It's a 12-byte BSON type designed to be unique and generally monotonically increasing. It consists of a timestamp, a machine ID, a process ID, and a counter, providing a high probability of uniqueness even across distributed systems.
5. What are the common data types in MongoDB?
MongoDB supports a rich set of BSON data types. Common ones include String, Number (integers, doubles, decimals), Boolean, Array, Object (nested documents), Date (stores UTC milliseconds since epoch), Binary data, ObjectId, Timestamp, Null, and Decimal128 for precise financial calculations. Understanding these types is crucial for efficient data storage and querying.
6. Describe CRUD operations in MongoDB.
CRUD stands for Create, Read, Update, and Delete – the fundamental operations for interacting with data in any database.
- Create:
db.collection.insertOne()ordb.collection.insertMany()to add new documents. - Read:
db.collection.find()to query and retrieve documents, optionally with criteria. - Update:
db.collection.updateOne(),updateMany(), orreplaceOne()to modify existing documents. - Delete:
db.collection.deleteOne()ordeleteMany()to remove documents.
// Example CRUD operations
db.users.insertOne({ name: "John Doe", age: 30 }); // Create
db.users.find({ name: "John Doe" }); // Read
db.users.updateOne({ name: "John Doe" }, { $set: { age: 31 } }); // Update
db.users.deleteOne({ name: "John Doe" }); // Delete
Indexes and Query Optimization
7. What is an index in MongoDB, and why is it important?
An index in MongoDB is a special data structure that stores a small portion of the collection's data in an easy-to-traverse form, typically a B-tree. It significantly improves query performance by allowing MongoDB to efficiently locate documents without scanning every document in a collection. Indexes are crucial for frequently queried fields, sorting operations, and enforcing uniqueness constraints. However, they consume storage space and add overhead to write operations.
8. Explain different types of indexes in MongoDB.
MongoDB supports various index types:
- Single Field Index: On a single field, ordered ascending or descending.
- Compound Index: On multiple fields; order matters for query efficiency.
- Multikey Index: For fields containing arrays, indexing each element.
- Geospatial Index: For geospatial query operations (2d, 2dsphere).
- Text Index: For full-text search capabilities.
- Hashed Index: For hash-based sharding.
- Wildcard Index: Indexes all fields or specific fields within documents.
- Partial Index: Indexes only documents that meet a specified filter expression.
- TTL Index: Automatically expires documents after a certain time.
9. How do you create an index, and what are its options?
Indexes are created using db.collection.createIndex(). The first argument specifies the fields to index and their sort order (1 for ascending, -1 for descending). The second argument is an options document. Common options include:
unique: trueto enforce uniqueness.sparse: trueto index only documents containing the indexed field.partialFilterExpression: { field: { $gt: 10 } }for partial indexes.name: "indexName"to specify a custom name.background: true(deprecated in newer versions, now default behavior for non-unique indexes).
// Example index creation
db.users.createIndex({ email: 1 }, { unique: true });
db.products.createIndex({ category: 1, price: -1 }); // Compound index
db.logs.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }); // TTL index
10. What is explain() and how is it used for query optimization?
The db.collection.find().explain() method provides detailed information about how MongoDB executes a query, including index usage, query plans, and execution statistics. It's a critical tool for query optimization. By analyzing the explain() output, specifically the winningPlan and executionStats sections, you can identify slow queries, missing indexes, inefficient index usage (e.g., collection scans), and optimize your queries or index strategies to improve performance.
// Example of using explain()
db.orders.find({ customerId: 123, status: "pending" }).explain("executionStats");
11. Explain covering indexes. Why are they beneficial?
A covering index is an index that includes all the fields necessary to fulfill a query and return the requested data, without needing to access the actual documents in the collection. When a query is "covered" by an index, MongoDB can return results directly from the index, which is significantly faster because it avoids disk I/O to read the full documents. This reduces latency and improves query performance, especially for queries returning many fields or large documents.
Replication and High Availability
12. What is a replica set in MongoDB? Why is it used?
A replica set is a group of MongoDB processes that maintain the same data set. It consists of one primary node and multiple secondary nodes. The primary node receives all write operations, while secondary nodes replicate the primary's data. Replica sets provide high availability (automatic failover if the primary fails) and data redundancy. They also enable read scalability by allowing read operations to be distributed among secondary members, though this requires careful configuration of read concerns.
13. Describe the role of the primary and secondary members in a replica set.
The primary member is the only node in a replica set that can accept write operations. It records all its operations in the oplog. Secondary members asynchronously replicate the primary's oplog, applying these operations to their own data sets to maintain data consistency. While secondaries can serve read operations, they cannot accept writes. If the primary becomes unavailable, an election process occurs, and one of the secondary members is promoted to primary.
14. What is an Oplog? How does it work?
The Oplog (Operation Log) is a special capped collection that records all write operations performed on the primary database. It functions as a rolling record of all changes to the data. Secondary members continuously poll the primary's oplog and apply these operations to their own data sets to stay synchronized. This mechanism ensures that all members of a replica set maintain an identical data state. The size of the oplog determines how far back a secondary can go to synchronize with the primary without a full data resync.
15. Explain write concerns and read concerns. How do they affect consistency and durability?
Write Concern specifies the level of acknowledgment requested from MongoDB for a write operation. It impacts durability and consistency.
w: 1(default): Acknowledges write when processed by primary.w: "majority": Acknowledges write only when applied to the majority of replica set members. Ensures higher durability. Read Concern specifies the consistency guarantee for read operations.local(default): Returns data from the local member, potentially stale.majority: Returns data that has been acknowledged by a majority of replica set members. Provides higher consistency.linearizable: Strongest read concern, ensures reads reflect a global order of operations.
Using stronger write and read concerns increases data consistency and durability but can impact performance.
// Example write concern
db.products.insertOne(
{ name: "Gadget A", price: 99.99 },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
);
// Example read concern
db.products.find({}).readConcern("majority");
Sharding and Scalability
16. What is sharding in MongoDB, and why is it used?
Sharding is a method for distributing data across multiple servers (shards) to support deployments with very large data sets and high throughput operations. It's MongoDB's approach to horizontal scaling. Each shard holds a subset of the total data. Sharding is used to overcome the memory and disk limitations of a single server, allowing for massive data growth and increased read/write capacity beyond what a single replica set can provide.
17. Explain the components of a sharded cluster.
A MongoDB sharded cluster consists of three main components:
- Shards: Each shard is a replica set that stores a subset of the sharded data. They handle data storage and query processing for their respective data partitions.
- Config Servers: These are special replica sets that store the cluster's metadata, including chunk ranges, shard locations, and authentication information. They are critical for the cluster's operation.
- Mongos (Query Routers): These are stateless routing processes that client applications connect to. Mongos instances determine which shard(s) to route queries or write operations to, aggregate results, and return them to the client.
18. What is a shard key? How do you choose an effective one?
A shard key is a field or a compound of fields within a document that MongoDB uses to distribute documents across shards. It's chosen when sharding a collection. An effective shard key is crucial for good cluster performance:
- Cardinality: High unique values to ensure even data distribution.
- Frequency: Even access patterns across shard key values.
- Monotonicity: Avoid choosing a field that monotonically increases or decreases (like
_idby default) for a ranged shard key, as it can lead to hot spots (all new writes going to one shard). Hashed shard keys are better for such cases. - Query isolation: Queries that include the shard key can be routed to a specific shard, avoiding scatter-gather operations.
// Example of sharding a collection with a compound shard key
sh.shardCollection("testdb.orders", { customerId: 1, orderDate: 1 });
// Example of sharding a collection with a hashed shard key
sh.shardCollection("testdb.users", { _id: "hashed" });
19. Describe the chunk and balancer processes in a sharded cluster.
Data in a sharded collection is divided into contiguous ranges called chunks. Each chunk has a minimum and maximum shard key value. The balancer is a background process (running on the config servers) that automatically migrates chunks between shards to ensure an even distribution of data. It prevents a single shard from becoming overloaded, which could happen if new data preferentially lands on one shard. The balancer runs periodically and can be enabled or disabled.
20. What is a zone in sharding? When would you use it?
Zones (or Tag-Aware Sharding) allow you to associate a range of shard key values with specific shards or groups of shards. This enables you to control data placement and ensure that certain data resides only on specific shards. You would use zones for:
- Data Locality: Keeping data geographically close to users for lower latency.
- Compliance: Meeting data residency requirements (e.g., GDPR).
- Workload Isolation: Directing specific workloads to dedicated hardware.
- Tiered Storage: Storing frequently accessed data on high-performance shards and less frequently accessed data on cheaper storage shards.
Aggregation Framework & Advanced Features
21. What is the Aggregation Framework? Name some common stages.
The Aggregation Framework is a powerful tool in MongoDB for performing advanced data processing and analysis. It allows you to transform documents through a multi-stage pipeline. Documents pass through stages, each performing an operation (filter, group, project, sort, etc.) and passing the results to the next stage. Common stages include:
$match: Filters documents.$project: Reshapes documents, selects fields, renames fields.$group: Groups documents by a specified key and performs aggregate functions.$sort: Sorts documents.$limit: Limits the number of documents.$skip: Skips a specified number of documents.$unwind: Deconstructs an array field from the input documents to output a document for each element.$lookup: Performs a left outer join to an unsharded collection in the same database.
// Example aggregation pipeline
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: "$customerId", totalOrders: { $sum: 1 }, totalAmount: { $sum: "$amount" } } },
{ $sort: { totalAmount: -1 } },
{ $limit: 10 }
]);
22. When would you use $lookup in the aggregation pipeline?
The $lookup stage performs a left outer join from one collection to another (an unsharded collection in the same database). It's used when you need to combine documents from two collections based on a common field, similar to a JOIN operation in SQL. $lookup is particularly useful for denormalizing data on the fly for a specific query, fetching related information (e.g., order details with customer information) without permanently embedding it, which can be inefficient if the related data is frequently updated or very large.
23. Explain MongoDB Transactions. When are they needed?
MongoDB supports multi-document ACID transactions across replica sets and sharded clusters. Transactions provide atomicity, consistency, isolation, and durability guarantees for operations involving multiple documents or multiple collections. They are needed when a set of operations must either all succeed or all fail together to maintain data integrity. For example, transferring money between two accounts: both the debit and credit operations must complete successfully within a single transaction.
// Example of a transaction
const session = db.getMongo().startSession();
session.startTransaction({
readConcern: { level: "majority" },
writeConcern: { w: "majority" }
});
try {
session.getDatabase("mydb").accounts.updateOne(
{ _id: "account1" },
{ $inc: { balance: -100 } },
{ session }
);
session.getDatabase("mydb").accounts.updateOne(
{ _id: "account2" },
{ $inc: { balance: 100 } },
{ session }
);
session.commitTransaction();
print("Transaction committed.");
} catch (error) {
session.abortTransaction();
print("Transaction aborted: " + error);
} finally {
session.endSession();
}
24. What are Change Streams? Provide a use case.
Change Streams allow client applications to access real-time data changes as they occur in a collection, database, or even an entire deployment. They provide a continuous, real-time feed of all modifications (inserts, updates, deletes, replaces) to your data.
A common use case is real-time analytics dashboards or microservice synchronization. For instance, a separate service could listen to changes on an orders collection, and upon a new order insertion, trigger an email notification, update an inventory service, or push the order to a shipping queue, all in real-time without polling the database.
25. Describe Time Series Collections. How do they optimize data storage and retrieval?
Time Series Collections are optimized collections in MongoDB (introduced in 5.0) specifically designed for efficiently storing and querying time-ordered data, such as sensor readings, stock prices, or event logs. They automatically group data points that occur close in time into single, compressed BSON documents, significantly reducing storage footprint and improving query performance for time-based queries. This optimization is done automatically by MongoDB, simplifying data modeling for time-series workloads.
System Design, Deployment, and Best Practices
26. How do you approach data modeling in MongoDB compared to a relational database?
In MongoDB, the approach shifts from normalization to denormalization and embedding related data within documents. While relational databases prioritize reducing data redundancy with many tables and joins, MongoDB often embeds arrays and sub-documents to keep related data together, minimizing the need for expensive joins (though $lookup exists). When embedding isn't suitable (e.g., one-to-many relationships where the "many" is unbounded or frequently updated independently), referencing (storing _ids of related documents) is used. The goal is to design schemas that align with application queries and data access patterns.
27. Discuss backup and restore strategies for MongoDB.
Several strategies exist for backup and restore:
- Filesystem Snapshots: For sharded clusters and replica sets, consistent backups can be achieved by pausing writes and taking filesystem snapshots of all members simultaneously.
mongodump/mongorestore: These utilities create BSON files or restore from them.mongodumpis good for logical backups of specific databases/collections. Can be slow for very large datasets and lacks point-in-time recovery.- MongoDB Atlas Backups: For Atlas deployments, continuous cloud backups provide point-in-time recovery with minimal operational overhead.
- Ops Manager/Cloud Manager: Provides automated, consistent, and scheduled backups for on-premise deployments with point-in-time recovery. The best strategy depends on RPO (Recovery Point Objective), RTO (Recovery Time Objective), and deployment type.
28. What are common security considerations for MongoDB deployments?
Security is paramount:
- Authentication: Enable authentication (SCRAM-SHA-256 is recommended) and create users with specific roles and privileges.
- Authorization: Implement Role-Based Access Control (RBAC) to grant granular permissions.
- Network Encryption: Enable TLS/SSL for all connections to encrypt data in transit.
- Network Hardening: Restrict network access to MongoDB instances using firewalls, only allowing trusted IP addresses or applications.
- Auditing: Enable auditing to track database events and suspicious activities.
- Data Encryption at Rest: Encrypt data files on disk (either via filesystem encryption or MongoDB's WiredTiger encryption).
- Regular Updates: Keep MongoDB version up-to-date to patch vulnerabilities.
29. How would you monitor a MongoDB deployment? What metrics are important?
Monitoring is crucial for performance and health. Key metrics include:
- Connections: Current and available connections.
- Operations: Insert, query, update, delete, getmore, command rates.
- Latency: Read/write latency, especially p99.
- Memory Usage: Resident memory, virtual memory, page faults.
- CPU Utilization: System and user CPU.
- Disk I/O: Read/write operations per second, queue depth.
- Replication Lag: Delay between primary and secondaries.
- Oplog Window: How much time is covered by the oplog.
- Index Usage: Hits, misses for indexes.
- WiredTiger Cache: Usage, eviction. Tools like MongoDB Atlas Monitoring, Ops Manager/Cloud Manager, Prometheus/Grafana, or custom scripts can be used.
30. When would you choose MongoDB over a relational database, and vice versa?
Choose MongoDB when:
- Rapid Development/Evolving Schemas: Data structure changes frequently, or data types vary.
- Large-scale Data/High Throughput: Need horizontal scalability (sharding) for massive data volumes and high read/write loads.
- Unstructured/Semi-structured Data: Ideal for IoT, content management, user profiles, catalogs where data doesn't fit rigid rows/columns.
- Cloud-Native Applications: Fits well with microservices and agile development.
Choose a Relational Database when:
- Strong ACID Compliance is Paramount: Strict transactional guarantees are absolutely critical across complex, multi-row operations.
- Highly Normalized Data: Data is best represented in many tables with complex relationships, and joins are frequent and efficient.
- Mature Ecosystem/Reporting: Extensive tools for complex analytics, reporting, and business intelligence are required, often with SQL expertise.
- Fixed Schemas: Data structure is well-defined and rarely changes.
Quick revision table
| Topic | One-line answer | Common follow-up |
|---|---|---|
| Document/BSON | Flexible, JSON-like key-value pairs (BSON for efficiency & types). | Why not just use plain JSON? |
_id / ObjectId | _id is unique primary key, ObjectId is a common, distributedly unique type for it. | Can I use a custom _id? What are its properties? |
| CRUD | Create, Read, Update, Delete basic operations. | How do updateOne and replaceOne differ? |
| Indexes | B-tree structures to speed up queries by avoiding collection scans. | When would an index not be used by a query? |
explain() | Shows query plan and execution stats to optimize queries and index usage. | What specific metrics in explain() indicate a bad query? |
| Covering Index | An index that satisfies a query and returns all requested fields from the index itself. | What are the performance benefits of a covering index? |
| Replica Set | Group of MongoDB processes for high availability and data redundancy. | How does automatic failover work? |
| Primary/Secondary | Primary takes writes, secondaries replicate and can serve reads. | Can secondaries take writes? How do they sync? |
| Oplog | Capped collection on primary logging all write operations for replication. | What happens if the oplog is too small? |
| Write/Read Concern | Defines durability/consistency guarantees for operations across replica sets. | How do w: "majority" and readConcern: "majority" impact performance? |
| Sharding | Horizontal scaling by distributing data across multiple servers (shards). | What are the limitations of sharding? |
| Sharded Components | Shards (data), Config Servers (metadata), Mongos (query router). | What happens if a config server fails? |
| Shard Key | Field(s) used to distribute documents among shards. | What makes a good shard key? What about hot spots? |
| Chunks/Balancer | Chunks are data ranges; balancer moves them to ensure even distribution. | When would you disable the balancer? |
| Zones | Associating shard key ranges with specific shards for data placement control. | Give a use case for zones. |
| Aggregation | Multi-stage pipeline for powerful data transformation and analysis. | When would you use aggregation over simple find()? |
$lookup | Performs a left outer join to combine data from another collection. | What are the performance considerations for $lookup? |
| Transactions | Multi-document ACID guarantees for atomic operations. | When are transactions absolutely necessary? |
| Change Streams | Real-time notifications of data changes in collections/databases. | How do change streams differ from polling? |
| Time Series Collections | Optimized for time-ordered data, reducing storage and improving queries. | How do they achieve better performance? |
| Data Modeling | Focus on embedding and referencing based on query patterns, less normalization. | When would you embed vs. reference? |
| Backup/Restore | Strategies include mongodump, filesystem snapshots, Atlas backups. | What's the difference between logical and physical backups? |
| Security | Auth, RBAC, TLS, network hardening, auditing, encryption. | What's the most critical security measure to implement? |
| Monitoring | Track connections, operations, latency, CPU, memory, replication lag, oplog. | How do you detect a slow query in production? |
| MongoDB vs. RDBMS | MongoDB for flexible schemas & scalability; RDBMS for strict ACID & complex joins. | When would a hybrid approach be beneficial? |
How to prepare
- Practice with the Shell: Get hands-on by running queries, aggregations, and administrative commands in the
mongoshCLI. - Understand Core Concepts Deeply: Don't just memorize definitions; grasp why MongoDB works the way it does (e.g., why BSON, why replica sets, why sharding).
- Review Data Modeling Patterns: Understand embedding vs. referencing, and how to choose based on access patterns, data relationships, and performance needs.
- Focus on Performance & Scalability: Be ready to discuss indexes,
explain(), replica sets, sharding, and how they contribute to a robust, performant system. - Simulate Scenarios: Think through how you'd design a schema for a given application, troubleshoot a performance issue, or ensure high availability in a practical context.
