MongoDB

MongoDB Interview Questions and Answers

SECTION 1: MongoDB Basics & Core Concepts

1. What is MongoDB and how does it differ from traditional RDBMS?
MongoDB is a NoSQL, document-oriented database that stores data in BSON (Binary JSON) format. Unlike RDBMS (Relational Database Management Systems), MongoDB does not rely on tables and rows. Instead, it uses collections and documents. Key differences include:

  • Schema-less structure

  • Horizontal scaling via sharding

  • Rich querying using JSON-like syntax

  • No native joins (though $lookup exists in aggregation)

2. What are BSON documents?
BSON stands for Binary JSON. It's a binary-encoded serialization of JSON-like documents. BSON extends JSON by including additional data types such as Date, ObjectId, and Binary, and it is optimized for speed.

3. What is the MongoDB data model?
MongoDB uses the following hierarchical model:

  • Database: Contains collections.

  • Collection: Contains documents (analogous to a table).

  • Document: A JSON-like structure that holds data (analogous to a row).

4. Explain the difference between a collection and a document.

  • A collection is a grouping of MongoDB documents, similar to a table.

  • A document is a single record in a collection and can have varying structures.

5. What is an ObjectId in MongoDB?
ObjectId is the default value for the _id field in MongoDB documents. It's a 12-byte identifier composed of:

  • 4 bytes: Timestamp

  • 5 bytes: Machine + Process ID

  • 3 bytes: Incrementing counter

6. How do you check the current database?

db

7. How do you switch to a different database?

use myDatabase

Explanation: This command switches the context to myDatabase. If it doesn’t exist, MongoDB creates it when data is inserted.

8. How do you list all databases?

show dbs

9. How do you list all collections in the current database?

show collections

10. How do you drop a database?

db.dropDatabase()

Explanation: Deletes the current database and all associated collections.

11. How do you create a collection?

db.createCollection("users")

Explanation: Creates a new empty collection named users.

12. What happens if you insert a document into a non-existent collection?
MongoDB automatically creates the collection upon the first insert operation.

13. How do you insert a document into a collection?

db.users.insertOne({ name: "John", age: 30 })

14. What are the different types of NoSQL databases?

  • Document-oriented (MongoDB)

  • Key-value stores (Redis)

  • Column-family stores (Cassandra)

  • Graph databases (Neo4j)

15. How is MongoDB schema-less?
MongoDB allows different documents in the same collection to have different fields or structures, providing flexibility during development.

16. What is the role of _id in a MongoDB document?
_id is a unique identifier for each document in a collection. It serves as the document's primary key and must be unique within the collection.

17. What is a capped collection?
A capped collection is a fixed-size collection that automatically overwrites its oldest entries when it reaches its size limit.

db.createCollection("logs", { capped: true, size: 10000 })

18. What are storage engines in MongoDB?
Storage engines handle how data is stored on disk:

  • WiredTiger (default): Supports document-level locking and compression.

  • MMAPv1 (deprecated): Older engine, uses memory-mapped files.

19. How do you check server status in MongoDB?

db.serverStatus()

Explanation: Returns diagnostic information and statistics about the server instance.

20. What command provides database statistics?

db.stats()

Explanation: Shows information such as the number of collections, objects, storage size, etc., for the current database.


SECTION 2: CRUD Operations & Indexing

1. How do you insert a single document into a collection?

db.users.insertOne({ name: "Alice", age: 28, email: "alice@example.com" })

Explanation: Inserts a single document into the users collection.

2. How do you insert multiple documents at once?

db.users.insertMany([
  { name: "Bob", age: 34 },
  { name: "Charlie", age: 22 }
])

Explanation: Efficient way to insert multiple documents in a single call.

3. How do you find all documents in a collection?

db.users.find()

Explanation: Returns a cursor with all documents in the users collection.

4. How do you pretty-print the output of a query?

db.users.find().pretty()

Explanation: Makes the JSON result more readable in the shell.

5. How do you find a document that matches a specific field?

db.users.find({ name: "Alice" })

Explanation: Matches all documents where the name is "Alice".

6. How do you update a document by adding a new field?

db.users.updateOne(
  { name: "Alice" },
  { $set: { city: "New York" } }
)

7. How do you update multiple documents?

db.users.updateMany(
  { age: { $gt: 30 } },
  { $set: { senior: true } }
)

8. How do you replace an entire document?

db.users.replaceOne(
  { name: "Charlie" },
  { name: "Charlie", age: 23, location: "LA" }
)

9. How do you delete a single document?

db.users.deleteOne({ name: "Bob" })

10. How do you delete multiple documents?

db.users.deleteMany({ age: { $lt: 25 } })

11. How do you count documents in a collection?

db.users.countDocuments()

12. How do you find documents with projection (specific fields only)?

db.users.find({}, { name: 1, _id: 0 })

Explanation: Returns only name field and excludes _id.

13. How do you sort query results?

db.users.find().sort({ age: -1 })

Explanation: Sorts users by age in descending order.

14. How do you limit the number of returned documents?

db.users.find().limit(5)

15. How do you skip a number of results?

db.users.find().skip(5)

16. How do you create an index on a field?

db.users.createIndex({ email: 1 })

Explanation: Speeds up queries that filter by the email field.

17. How do you create a compound index?

db.users.createIndex({ age: 1, name: -1 })

Explanation: Index on multiple fields with different sort directions.

18. How do you view all indexes on a collection?

db.users.getIndexes()

19. How do you drop an index?

db.users.dropIndex("email_1")

20. What is a unique index and how do you create one?

db.users.createIndex({ email: 1 }, { unique: true })

Explanation: Ensures no two documents have the same email value.

21. What is the default index in a MongoDB collection?
Every collection has a default index on the _id field:

db.users.getIndexes()

You'll always see an index { _id: 1 }.


Next: Section 3 - Aggregation Framework & Pipelines

SECTION 3: Aggregation Framework & Pipelines

1. What is aggregation in MongoDB?
Aggregation is a way to process data records and return computed results. It involves operations like filtering, grouping, projecting, and sorting using a pipeline of stages.

2. What is an aggregation pipeline?
An aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. It consists of multiple stages such as $match, $group, $sort, $project, etc.

3. How do you use the $match stage?

db.orders.aggregate([
  { $match: { status: "shipped" } }
])

Explanation: Filters documents to only include those with status equal to "shipped".

4. How do you group data using $group?

db.orders.aggregate([
  { $group: { _id: "$customerId", totalAmount: { $sum: "$amount" } } }
])

Explanation: Groups documents by customerId and calculates the total amount spent.

5. What does $project do in aggregation?

db.users.aggregate([
  { $project: { name: 1, email: 1, _id: 0 } }
])

Explanation: Specifies which fields to include or exclude in the output.

6. How do you sort documents in an aggregation pipeline?

db.users.aggregate([
  { $sort: { age: -1 } }
])

Explanation: Sorts documents by age in descending order.

7. How do you limit the number of results in a pipeline?

db.users.aggregate([
  { $limit: 5 }
])

8. How do you skip documents in an aggregation?

db.users.aggregate([
  { $skip: 5 }
])

9. How do you calculate average using $avg in $group?

db.orders.aggregate([
  { $group: { _id: "$productId", avgAmount: { $avg: "$amount" } } }
])

10. How do you use multiple stages in a pipeline?

db.users.aggregate([
  { $match: { age: { $gt: 25 } } },
  { $sort: { age: 1 } },
  { $project: { name: 1, age: 1 } }
])

11. What is $unwind and how is it used?

db.users.aggregate([
  { $unwind: "$hobbies" }
])

Explanation: Deconstructs an array field from the input documents to output a document for each element.

12. How do you use $lookup for joining collections?

db.orders.aggregate([
  {
    $lookup: {
      from: "customers",
      localField: "customerId",
      foreignField: "_id",
      as: "customerDetails"
    }
  }
])

Explanation: Performs a left outer join with the customers collection.

13. How do you filter documents after a $lookup?
Use $unwind and $match after $lookup to filter joined results:

db.orders.aggregate([
  { $lookup: { ... } },
  { $unwind: "$customerDetails" },
  { $match: { "customerDetails.status": "active" } }
])

14. How do you count documents using aggregation?

db.users.aggregate([
  { $count: "totalUsers" }
])

15. How do you use $addFields?

db.users.aggregate([
  { $addFields: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
])

Explanation: Adds a new field or modifies an existing one.

16. How do you calculate the maximum value in a group?

db.orders.aggregate([
  { $group: { _id: null, maxAmount: { $max: "$amount" } } }
])

17. How do you calculate the minimum value in a group?

db.orders.aggregate([
  { $group: { _id: null, minAmount: { $min: "$amount" } } }
])

18. How do you use conditional logic in aggregation?

db.users.aggregate([
  {
    $project: {
      name: 1,
      ageGroup: {
        $cond: { if: { $gte: ["$age", 18] }, then: "Adult", else: "Minor" }
      }
    }
  }
])

19. How do you combine aggregation and update?
Use aggregation to build a pipeline and apply it in $merge:

db.sales.aggregate([
  { $group: { _id: "$region", total: { $sum: "$amount" } } },
  { $merge: "regional_totals" }
])

Explanation: Aggregates data and merges the result into regional_totals.

20. How do you use variables inside aggregation pipelines?
Use the $let operator inside $project:

db.users.aggregate([
  {
    $project: {
      fullName: {
        $let: {
          vars: { full: { $concat: ["$firstName", " ", "$lastName"] } },
          in: "$full"
        }
      }
    }
  }
])

SECTION 4: Replication and High Availability

1. What is replication in MongoDB?
Replication is the process of synchronizing data across multiple servers (nodes) for redundancy and high availability. It uses replica sets.

2. What is a replica set?
A replica set is a group of mongod instances that maintain the same dataset. One node is primary, and the others are secondaries.

3. How many nodes are required for a replica set to elect a primary?
An odd number of voting members is recommended. At least 3 nodes are typical to allow a majority vote.

4. How do you initiate a replica set?

rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "localhost:27017" },
    { _id: 1, host: "localhost:27018" },
    { _id: 2, host: "localhost:27019" }
  ]
})

5. How do you check the replica set status?

rs.status()

6. How does MongoDB choose a primary in a replica set?
Primary is elected using a consensus (majority vote) among voting nodes based on availability and priority.

7. What is oplog (operations log)?
It’s a special capped collection (local.oplog.rs) that records all changes applied to the primary and gets replayed by secondaries.

8. Can you read from secondaries?
Yes, with read preference:

db.getMongo().setReadPref("secondary")

9. What is a hidden member in a replica set?
A member that receives replication but does not participate in elections and cannot become primary. Used for backups or analytics.

{ _id: 3, host: "localhost:27020", hidden: true, priority: 0 }

10. What is an arbiter node?
An arbiter doesn't store data but helps break election ties.

{ _id: 4, host: "localhost:27021", arbiterOnly: true }

11. What happens if a primary goes down?
An election is triggered among secondaries to select a new primary. Writes resume once a new primary is elected.

12. How do you reconfigure a replica set?

cfg = rs.conf()
cfg.members.push({ _id: 5, host: "localhost:27022" })
rs.reconfig(cfg)

13. What is write concern in replication?
Defines the level of acknowledgment requested from MongoDB for write operations:

db.collection.insertOne(doc, { writeConcern: { w: "majority" } })

14. How do you check the current primary?

rs.isMaster()

15. What does rs.conf() return?
Returns the current replica set configuration.

16. What is read preference in replication?
Specifies from which members queries should read data:

  • primary

  • secondary

  • primaryPreferred

  • secondaryPreferred

  • nearest

17. Can secondaries be used for backup?
Yes, hidden or delayed secondaries are commonly used for backup to avoid impacting production traffic.

18. How do you monitor replication lag?
Use rs.printSlaveReplicationInfo() to check delay in seconds.

19. What is the role of priority in replication?
Determines the likelihood of a node becoming primary. Nodes with higher priority are preferred.

20. Can you force a step down of a primary?
Yes:

rs.stepDown()

21. What is chained replication?
Secondaries replicate from other secondaries instead of directly from the primary. Enabled by default.

22. How do you disable chaining?

rs.reconfig({ ... , settings: { chainingAllowed: false } })

SECTION 5: Sharding and Horizontal Scaling

1. What is sharding in MongoDB?
Sharding is the process of storing data records across multiple machines to support deployments with large data sets and high throughput operations.

2. Why is sharding needed?
It allows horizontal scaling, distributing the data across multiple servers to prevent any single server from becoming a bottleneck.

3. What are the components of a sharded cluster?

  • Shard: Contains subset of data.

  • Mongos: Query router.

  • Config servers: Store metadata and configuration settings.

4. How do you enable sharding on a database?

sh.enableSharding("myDatabase")

5. How do you shard a collection?

sh.shardCollection("myDatabase.myCollection", { userId: 1 })

Explanation: Shards myCollection based on the userId field.

6. What is a shard key?
A field used to distribute data across shards. Choosing the right shard key is critical for even data distribution.

7. What are the types of shard keys?

  • Hashed shard key

  • Ranged shard key

8. How do hashed shard keys work?
Data is distributed based on the hash of the shard key’s value.

sh.shardCollection("db.coll", { userId: "hashed" })

9. What is the balancer in MongoDB?
A background process that redistributes data among shards to maintain even data distribution.

10. How do you check balancer status?

sh.isBalancerRunning()

11. How do you manually stop/start the balancer?

sh.stopBalancer()
sh.startBalancer()

12. How do you check the sharding status?

sh.status()

13. What happens if a shard goes down?
The cluster remains available if the data is replicated. Queries targeting that shard may fail if data isn’t replicated elsewhere.

14. How does the mongos router work?
Mongos routes client queries to the appropriate shard(s) based on the shard key.

15. Can you change the shard key of a collection?
No, MongoDB does not support changing the shard key once defined.

16. What is a chunk in sharding?
A chunk is a range of shard key values. MongoDB automatically splits and migrates chunks among shards.

17. How do you view chunk distribution?

use config
db.chunks.find({ ns: "myDatabase.myCollection" })

18. What is chunk splitting?
Chunks are split automatically when they grow too large, or manually using:

sh.splitAt("myDatabase.myCollection", { userId: 5000 })

19. What is chunk migration?
MongoDB moves chunks between shards to balance data distribution.

20. How do you manually move a chunk?

sh.moveChunk("myDatabase.myCollection", { userId: 5000 }, "shard0001")

21. How do you monitor sharded cluster performance?
Use mongostat, mongotop, and diagnostic commands like db.serverStatus(), db.printShardingStatus().

22. What are orphaned documents in sharding?
Documents present in a shard that do not belong there according to the current chunk distribution. Cleanup with cleanupOrphaned command.

SECTION 6: Performance Tuning and Optimization

1. What is performance tuning in MongoDB?
It’s the process of optimizing queries, indexes, and system configurations to improve the efficiency of database operations.

2. How do you identify slow queries?
Use the slow query log or db.currentOp():

db.setProfilingLevel(1)
db.system.profile.find({ millis: { $gt: 100 } })

3. What is MongoDB’s profiler?
A tool to monitor database operations:

db.setProfilingLevel(2)
db.system.profile.find().limit(5).sort({ ts: -1 })

4. How do you analyze query performance?
Use explain():

db.collection.find({ field: "value" }).explain("executionStats")

5. What is an index in MongoDB?
Indexes support efficient query execution. Without indexes, MongoDB performs a collection scan.

6. How do you view existing indexes?

db.collection.getIndexes()

7. How do you create a compound index?

db.collection.createIndex({ lastName: 1, firstName: 1 })

8. What is index cardinality?
It refers to the uniqueness of values in the indexed field. High cardinality indexes are more selective and performant.

9. What is a covered query?
A query where all fields in the query, projection, and sort are covered by an index. It avoids reading documents.

10. What is the working set in MongoDB?
The set of data and indexes that are frequently accessed and ideally fit in RAM.

11. How do you monitor memory usage?
Use:

db.serverStatus().mem

12. How do you monitor cache usage?
Use WiredTiger cache stats:

db.serverStatus().wiredTiger.cache

13. What is the impact of large documents on performance?
Large documents increase I/O, memory usage, and can cause inefficiencies in updates and reads.

14. What is projection in MongoDB?
Limiting the fields returned:

db.collection.find({}, { name: 1, _id: 0 })

15. How do you improve write performance?

  • Use bulkWrite

  • Use appropriate writeConcern

  • Avoid frequent index updates

16. How do you improve read performance?

  • Use indexes

  • Use covered queries

  • Avoid large documents and unneeded fields

17. What is the role of $hint in MongoDB?
Force MongoDB to use a specific index:

db.collection.find({ age: 25 }).hint({ age: 1 })

18. What is $natural sorting?
Returns documents in the order they were stored:

db.collection.find().sort({ $natural: 1 })

19. What are some schema design tips for performance?

  • Design for application needs

  • Avoid joins by embedding when possible

  • Use references when data is accessed independently

20. How do you benchmark MongoDB performance?
Use tools like mongostat, mongotop, and benchRun():

benchRun({ ops: [{ op: "find", ns: "test.coll", query: {} }], seconds: 10 })

21. How do you avoid collection scans?
Ensure queries are supported by appropriate indexes.

22. What is the use of the WiredTiger engine for performance?
It provides document-level concurrency and compression for better memory and disk efficiency.

23. How do TTL indexes help performance?
Automatically delete expired documents to reduce data size and improve efficiency:

db.logs.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })

24. What are partial indexes?
Indexes only part of a collection:

db.collection.createIndex({ status: 1 }, { partialFilterExpression: { status: { $eq: "active" } } })

25. What is analyzeShardKey in MongoDB 6.0+?
Used to evaluate the effectiveness of a shard key before sharding:

db.adminCommand({ analyzeShardKey: "db.coll", key: { userId: 1 } })

Scroll to Top