Database · Fresher-relevant
MongoDB interview questions on document model, aggregation pipeline, indexing, and schema design for NoSQL applications.
What is the difference between embedding and referencing in MongoDB?
Tip: Embedding nests related data in one document — fast reads, atomic updates, but risks duplication and the 16MB limit. Referencing stores an ObjectId pointing to another collection — better for large or independently-changing data, at the cost of extra lookups ($lookup). Model around access patterns.
How does the aggregation pipeline work?
Tip: Documents flow through ordered stages — $match, $group, $project, $sort, $lookup, $unwind — each transforming the stream. Place $match and $sort early (and back them with indexes) to reduce documents before expensive stages.
How does indexing work in MongoDB and what is a compound index?
Tip: MongoDB uses B-tree indexes; the _id index is automatic. A compound index on {a:1, b:1} follows the leftmost-prefix rule and can serve queries on a, or a+b, but not b alone. Use .explain("executionStats") to verify an index is used (IXSCAN vs COLLSCAN).
What guarantees do MongoDB transactions and write concerns provide?
Tip: Single-document writes are always atomic. Multi-document ACID transactions are supported on replica sets/sharded clusters but add cost. writeConcern controls durability (w:"majority" waits for a majority of nodes); readConcern controls visibility. Tune them per operation criticality.
What is sharding and how does the shard key affect performance?
Tip: Sharding horizontally partitions data across nodes by a shard key. A poor key (monotonic, like a timestamp) creates a hotspot on one shard; a high-cardinality, evenly-distributed key (often hashed) spreads load. The key is hard to change later, so choose carefully.
When should you choose MongoDB over a relational database?
Tip: Choose MongoDB for flexible/evolving schemas, hierarchical or document-shaped data, and high write throughput with horizontal scaling. Choose an RDBMS for strong multi-row transactional integrity, complex ad-hoc joins, and well-defined relational data. It is a modelling decision, not a "NoSQL is better" one.
What is the difference between find() and aggregate()?
Tip: find() retrieves documents matching a filter with simple projection and sort. aggregate() runs a multi-stage pipeline for grouping, joins ($lookup), reshaping, and computed fields. Use find for straightforward reads, aggregate for analytics and transformations.
How do replica sets provide high availability?
Tip: A replica set has one primary (takes writes) and secondaries that replicate its oplog. If the primary fails, an election promotes a secondary automatically. Reads can be routed to secondaries for scale, accepting eventual consistency unless readPreference/readConcern enforces stronger guarantees.
InterviewEra generates role-specific questions using your actual projects and skills. Get scored feedback on technical depth, clarity, and structure — free to start.