MongoDB is the most popular document database, used for web apps, content platforms, IoT and analytics. These MongoDB interview questions and answers cover the data model, queries, indexes, aggregation, scaling, transactions and security, with short examples in the MongoDB shell.

MongoDB interview questions and answers

Basics

1. What is MongoDB?

MongoDB is a NoSQL document database. It stores data as flexible, JSON-like documents grouped into collections, instead of rows in fixed tables.

2. Explain documents, collections and databases.

A document is a record made of field-value pairs. A collection is a group of documents, similar to a table. A database holds collections. Documents in one collection do not need identical fields, although you can enforce rules with schema validation.

3. What is BSON?

BSON is the binary format MongoDB uses to store documents. It supports more types than JSON, such as dates, 64-bit integers and binary data. A single document can be up to 16 MB.

4. When would you choose MongoDB over a relational database?

Choose it when data is naturally hierarchical, the schema changes often, or you need to scale horizontally. On the other hand, for many relationships and complex joins, a relational database such as PostgreSQL often fits better.

CRUD and queries

5. Show basic CRUD operations.

db.users.insertOne({ name: "Asha", city: "Ahmedabad", age: 29 })
db.users.find({ city: "Ahmedabad" }, { name: 1, _id: 0 })
db.users.updateOne({ name: "Asha" }, { $set: { age: 30 } })
db.users.deleteOne({ name: "Asha" })

The second argument to find() is a projection: it chooses which fields to return.

6. What is the _id field?

Every document has a unique _id. If you do not set one, MongoDB creates an ObjectId, which includes a timestamp.

7. Name some query operators.

$eq, $gt, $lt, $in, $and, $or, $exists and $regex. For updates, common operators are $set, $inc, $push and $unset.

Indexes and aggregation

8. Which index types does MongoDB support?

Single-field, compound, multikey (for arrays), text, geospatial, hashed, wildcard and TTL indexes. A TTL index deletes documents automatically after a set time, which suits sessions and logs.

9. How do you check whether a query uses an index?

Run .explain("executionStats") and look for an IXSCAN stage instead of a COLLSCAN (full collection scan).

10. What is the aggregation pipeline?

It processes documents through stages, such as $match, $group, $sort, $project and $lookup (a left outer join).

db.orders.aggregate([
  { $match: { status: "paid" } },
  { $group: { _id: "$city", total: { $sum: "$amount" } } },
  { $sort: { total: -1 } }
])

Scaling and reliability

11. What is a replica set?

A replica set is a group of servers that hold the same data. One primary accepts writes, and secondaries replicate them. If the primary fails, the members elect a new one automatically.

12. What is sharding?

Sharding splits a collection across several replica sets using a shard key. A good shard key spreads data and queries evenly, while a poor one creates hot spots.

13. Does MongoDB support transactions?

Yes. Since version 4.0, it supports multi-document ACID transactions on replica sets, and since 4.2, on sharded clusters. Even so, well-designed documents often avoid the need for them.

14. Which storage engine does MongoDB use?

WiredTiger is the default. It provides document-level concurrency and compression. The old MMAPv1 engine was removed in version 4.2.

15. What are read and write concerns?

They control consistency and durability. For example, writeConcern: { w: "majority" } waits until most replica set members confirm the write.

Security

16. How do you secure MongoDB?

  • Enable authentication and role-based access control.
  • Bind to private interfaces, and never expose port 27017 to the internet.
  • Use TLS for connections.
  • Encrypt data at rest.
  • Keep MongoDB updated and audit access.

Many data leaks have come from MongoDB servers left open without passwords.

Keep preparing

Practise each answer out loud, and run the commands in a lab rather than memorising them. Interviewers often ask follow-up questions, so make sure you understand why each answer is true.

Sources

Preparing for an interview? Browse more questions in our interview preparation hub. For businesses, we also offer security services such as VAPT, server hardening and ISO 27001 readiness.

Post author avatar

Bhaskar Soni

Bhaskar Soni is a Security Engineer and the founder of Youngster Company, based in Ahmedabad, Gujarat. He works hands-on with Linux server hardening, vulnerability assessment and penetration testing (VAPT), ISO 27001 compliance and OSINT investigations. He writes practical, tested guides for IT professionals and Indian businesses, and every article on this site is written or reviewed by him.

Leave a Reply