MongoDB Interview Questions & Answers


🔹 1. What is MongoDB?

MongoDB is a NoSQL database that stores data in document format (JSON-like BSON) instead of tables.


🔹 2. Why is MongoDB used?

MongoDB is used because it is:

  • Flexible (schema-less)
  • Scalable
  • Fast for large data
  • Easy to integrate with modern apps

🔹 3. What is NoSQL database?

NoSQL databases store data in non-tabular form like:

  • Documents (MongoDB)
  • Key-value pairs
  • Graphs

🔹 4. What is a document in MongoDB?

A document is a record stored in BSON format (similar to JSON).

Example:

{
"name": "John",
"age": 25,
"city": "Mumbai"
}

🔹 5. What is a collection?

A collection is a group of documents (like a table in SQL).


🔹 6. Difference between SQL and MongoDB?

SQLMongoDB
TablesCollections
RowsDocuments
Fixed schemaFlexible schema

🔹 7. What is BSON?

BSON (Binary JSON) is the format MongoDB uses to store data internally.


🔹 8. What is schema-less database?

MongoDB does not require fixed structure; each document can have different fields.


🔹 9. What is _id in MongoDB?

It is a unique identifier automatically created for each document.


🔹 10. What is CRUD in MongoDB?

CRUD operations:

  • Create
  • Read
  • Update
  • Delete

🔹 Basic Commands

11. Insert document

db.users.insertOne({ name: "Amit", age: 22 })

12. Find documents

db.users.find()

13. Update document

db.users.updateOne(
{ name: "Amit" },
{ $set: { age: 23 } }
)

14. Delete document

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

15. Insert multiple documents

db.users.insertMany([
{ name: "A", age: 20 },
{ name: "B", age: 25 }
])

🔹 Intermediate Questions

16. What is indexing?

Indexing improves search performance in MongoDB.


17. Why is indexing important?

It makes queries faster and efficient.


18. What is aggregation?

Aggregation processes data and returns computed results.


19. What is aggregation pipeline?

A sequence of stages like filtering, grouping, sorting.


20. What is $match?

Used to filter documents.


21. What is $group?

Used to group documents by a field.


22. What is replica set?

A group of MongoDB servers that maintain same data for backup.


23. What is sharding?

Splitting large data across multiple servers.


24. What is horizontal scaling?

Adding more machines to handle load.


25. What is vertical scaling?

Increasing power of a single machine.


🔹 Advanced Questions

26. What is ACID property?

MongoDB supports ACID (Atomicity, Consistency, Isolation, Durability) in transactions.


27. Does MongoDB support transactions?

Yes, from version 4.0+.


28. What is replication?

Copying data across multiple servers.


29. What is write concern?

It defines acknowledgment level for write operations.


30. What is read preference?

It defines from which server data should be read.


31. What is embedded document?

A document inside another document.


32. What is referencing?

Linking documents using IDs.


33. Embedded vs Referencing?

EmbeddedReferencing
Faster readsBetter normalization
Less flexibleMore scalable

34. What is TTL index?

Automatically deletes documents after time expires.


35. What is capped collection?

Fixed-size collection that overwrites old data.


🔹 Node.js + MongoDB Questions

36. How does MongoDB connect with Node.js?

Using MongoDB driver or Mongoose.


37. What is Mongoose?

ODM (Object Data Modeling) library for MongoDB.


38. What is schema in Mongoose?

Structure definition for MongoDB documents.


39. What is model in Mongoose?

A compiled schema used to interact with database.


40. What is middleware in Mongoose?

Functions that run before or after database operations.


🔹 Performance Questions

41. How to improve MongoDB performance?

  • Use indexing
  • Optimize queries
  • Use proper schema design

42. What is explain() in MongoDB?

Shows query execution plan.


43. What is caching in MongoDB?

Storing frequently used data for faster access.


44. What is bulk write?

Performing multiple operations in a single request.


45. What is connection pooling?

Reusing database connections for efficiency.


🔹 Real Interview Questions

46. Build REST API using MongoDB?

Use Node.js + Express + MongoDB.


47. How does MongoDB handle big data?

Using sharding and horizontal scaling.


48. When to use MongoDB?

When data is:

  • Unstructured
  • Rapidly changing
  • Large-scale

49. What are disadvantages of MongoDB?

  • No complex joins
  • Memory usage can be high
  • Not ideal for relational data

50. Why MongoDB is popular?

Because it is:

  • Fast
  • Flexible
  • Scalable
  • Easy to use with modern apps