Latest MongoDB Interview Questions & Answers (2025):
1. Does MongoDB support ACID transactions across multiple documents/collections?
Yes, since version 4.0, MongoDB supports multi-document ACID transactions for replica sets, and from 4.2 onwards, for sharded clusters. Use startSession() and withTransaction() in drivers or shell to manage atomicity and rollbacks.
2. What is the aggregation pipeline and why is it powerful?
Aggregation pipelines allow data transformation via multiple stages ($match, $group, $project, $sort, $limit, etc.), supporting operations like grouping, filtering, and calculating metrics, similar to SQL GROUP BY but with greater flexibility and scalability.
3. Explain sharding vs. replication.
Replication (replica sets) provides high availability; data is copied to multiple nodes. Sharding enables horizontal scaling by splitting data across multiple servers (“shards”) to manage massive datasets and concurrent workloads.
4. What are TTL (Time-To-Live) indexes?
TTL indexes automatically remove expired documents after a predefined time period, great for logs and session data cleanup.
5. What is the WiredTiger storage engine?
WiredTiger is the default since MongoDB 3.2, offering document-level locking, compression, and improved concurrency. It replaced MMAPv1 for better performance under heavy loads.
6. Compound indexes – when should you use them?
Compound indexes index multiple fields in a specified order, improving query speed for filtering/sorting involving several fields.
7. What’s the difference between $lookup and $graphLookup in aggregations?$lookup is used for left outer joins between collections. $graphLookup performs recursive searches—useful for hierarchical data.
8. Describe write concern and read concern.
Write concern determines acknowledgment required for write ops (e.g., “majority” means most replicas); read concern ensures a consistent snapshot for reads.
9. How do you optimize slow MongoDB queries?
- Create relevant indexes.
- Project only needed fields.
- Use the aggregation pipeline for complex querying.
- Analyze execution plans via
explain(). - Split large documents when possible.
10. What is the upsert operation, and where is it used?
Upsert updates a matching document or inserts a new one if it doesn’t exist—commonly used in update workflows.
11. What is the role of Mongos in sharded clusters?mongos routes queries from clients to appropriate shards, providing a transparent interface for sharding.
12. How do you perform text search in MongoDB?
Create a text index and use the $text query operator for searching keywords in string fields.
13. How do you connect a Node.js app to MongoDB using Mongoose?
Install Mongoose, define schemas, connect using mongoose.connect(), and interact via model methods (find(), save(), etc.).
14. What steps do you follow migrating data from SQL to MongoDB?
Map relational tables to documents, export as JSON/BSON, import using mongoimport, validate and optimize indexed/query fields.
15. How do you monitor MongoDB performance?
Use Atlas/Ops Manager dashboards, monitor with db.currentOp(), analyze logs, set alerts for replica set failover, and use slow query logs.
Scenario-Based & Advanced Questions (2025 Focus):
- Troubleshoot replica set failover steps, memory spikes, timeouts.
- How do you handle schema changes with evolving apps?
- Describe strategies for scaling MongoDB in high-traffic use cases.
- Node.js and MongoDB validation and error handling.
Bangalore Job Seekers Tips for 2025:
- Prepare hands-on aggregation pipeline tasks (real-world data questions).
- Know latest indexing, scaling, monitoring, Atlas/cloud features.
- Practice schema migration and performance tuning.
- Stay up-to-date with MongoDB documentation and new features.