Ever wanted to get the best discounts? Get up to 20% off. View offers

MongoDB and Interview Questions

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

MongoDB Interview Questions and Answers

Posted in MongoDB, Interview Questions

Table of Contents

MongoDB is a document-based database created in 2007 by 10gen software. It works on the concept of collections and documents. A MongoDB server can contain multiple databases and offers high performance along with redundancy and easy scalability. Collection in MongoDB can be considered as a group of documents that can have different types of fields. The document is a set of key-value pairs having a dynamic schema i.e. common fields may hold different types of data and all documents in the same collection need not have the same structure. If you are new to MongoDB but have worked with RDBMS like SQL before, this small table will help you correlate the terminologies:

Table Collection
Row Document
Column Field
Table join Embedded documents

A few prominent features of MongoDB are –

  • Cross-platform
  • Written in C++.
  • Secure and scalable
  • Schema-less database
  • Document-oriented
  • Supports high-availability and redundancy

Top MongoDB Interview Questions and Answers

Here you can start your 

Question: What is Mongo shell?

Answer: Mongo shell is a JavaScript interface to MongoDB that can be used to query and update data. It is interactive and can also be used to do administrative operations.

Question: How does MongoDB store data?

Answer: Being document-based, MongoDB stores documents in BSON or Binary JavaScript Object notation which is the binary encoded format of JSON.

Question: What type of NoSQL database is MongoDB?

Answer: It is a document database that contains documents having key-value pairs, key-array pairs, and nested documents.

Question: List out the important features of MongoDB.

Answer: Important features of MongoDB are –

  • It is a schema-less database
  • No complex joins
  • Faster access to data because of the presence of the working set (internal memory)
  • Features like aggregation, sharding, and replication make it easy to use.
  • Cross-platform and document-based
  • Automatic fail-over and high-availability

Question: What are the data models in MongoDB?

Answer: Data modeling depends on the structure of documents. In MongoDB,

  • Related data can be embedded in a single document structure (Embedded data model)
  • Through references from one document to another, the relationship between data is stored (Normalized data model)

Question: MongoDB is called a schema-less database. Is it true? How to create the schema in MongoDB?

Answer: It would be more appropriate to say that MongoDB has dynamically typed schema because it relies on JSON which is a schema-free data structure. To create a schema, create and insert a document. Once a document is inserted, a corresponding collection will be created in the database.

Question: What is a namespace in MongoDB?

Answer: A namespace is the concatenation of the database name and collection name. Example – students.the subject, where students are the database and subject, is the collection.

Question: How do you perform CRUD operations in MongoDB?


  • C – Create – db.collection.insert();
  • R – Read – db.collection.find();
  • U – Update – db.collection.update();
  • D – Delete – db.collection.remove({“fieldname” : ”value”});

Question: What are the differences between MySQL and MongoDB?


Written in C and C++ Written in C++ and JavaScript
Follows RDBMS database structure Document-based structure
Scaling is done vertically Horizontal scaling
Rigidly defined data-structure, the schema is strict and should be defined in the beginning. Dynamic and flexible schema creation of complex documents.
Uses a Structured Query language The unstructured query language is used.
Uses Join to link data from two or more tables There is no equivalent for JOIN.
Performance is slow with a large database with unstructured data. Handles large unstructured data efficiently thereby giving a good performance.
Less suitable for a cloud-based environment The best option for services that are cloud-based.

Question: How are constraints managed in MongoDB?

Answer: You can add document validator on the collections starting MongoDB 3.2. Unique indexes can also be created using db.collection.createIndex({“key” : 1} , );

Question: What is ObjectId? How is it structured?

Answer: ObjectId is a class which is the default primary key for the document in MongoDB. It is found in the _id field of the inserted document. It is a 12-byte BSON type generated using a default algorithm. The structure is –

  • A 4-byte value that represents seconds since Unix epoch
  • 3-byte machine id
  • 2-byte process id
  • 3-bytes counter that starts with a random number

Question: What is BSON?

Answer: BSON or binary JSON is the binary encoded format of JSON. It extends JSON and provides more data types and fields.

Question: How does MongoDB do indexing?

Answer: Indexing is done using ensureIndex() method. Syntax –


Question: Name the default index created for a new collection.

Answer: Default index created for the new collection is _id

Question: What is sharding in MongoDB?

Answer: MongoDB meets the data growth using Sharding, which means sorting data records across various machines. Each shard is a replica set.

Question: What is a covered query and why is it important?

Answer: In a covered query, all the fields used in the query have the index created. The results returned should also be part of the index. Due to this, MongoDB fetches the results without actually looking inside documents, thus saving time and increasing efficiency.

Question: What is replication and what are the primary and secondary replica sets?

Answer: Replication means synchronizing the data across multiple servers. It increases data availability. If a single server is lost, data is still intact in the other servers.

  • Primary replica set – MongoDB writes data only to primary or master replica set.
  • Secondary replica set – secondary or slave nodes can accept only reads. They replicate from the primary.

Question: Name the 2 storage engines using MongoDB?

Answer: MMAPv1 and WiredTiger are the 2 storage engines used by MongoDB.

Question: How to create and drop a collection in MongoDB?


  • Create collection – db.createCollection();
  • Drop collection – db.collection.drop();

Question: What is journaling and how does it work?

Answer: MongoDB ensures data integrity by using an on-disk journal that is created for every write. In case of a server crash, the journal can be used to track the writes that were not written to the disk or data files.

Journal Mechanics in MongoDB

Question: How can you store images, videos and other large files in MongoDB?

Answer: Large files are stored in MongoDB using the GridFS specification.

Question: Compare CouchDB and MongoDB.


CouchDB MongoDB
The data model is document oriented i.e. JSON Uses BSON which is an extension of JSON
Uses HTTP/REST for querying Uses standard protocol over TCP/IP
Database has documents The database contains collections and collections contain documents
Written in Erlang Written in C++
Uses range queries and map/reduce Uses object-based query language and map/reduce

Question: In what ways is MongoDB better than MySQL? Explain.

Answer: MongoDB has a flexible data model wherein the schema can be expanded based on business needs. Also, MongoDB is faster and can handle more data types to manage real-time applications better.

Question: What are the different index types in MongoDB?


  • Default – this is the _id that MongoDB creates
  • Single field – for indexing and sorting on a single field
  • Compound – for multiple fields
  • Multi-key – for indexing array data
  • Hashed – indexes the hashes of a field value
  • Geospatial – to query geospatial(location) data

Question: What is an ACID transaction? Does MongoDB support it?

Answer: ACID stands for Atomicity, Consistency, Isolation, and Durability. The transaction manager ensures these attributes are taken care of. Yes, MongoDB version 4.0 supports ACID.

Question: Explain the role of profiler in MongoDB.

Answer: A profiler is an in-built tool that finds out the slow and resource-intensive queries and provides query-level insights. You as a dba can analyze the queries. Profiler stores all the data in system.profile collection.

Question: How does MongoDB handle transactions and locks?

Answer: MongoDB uses multi-granularity locking wherein operations can be locked at the global, database or collection level. It is up to the storage engines to implement the level of concurrency. For example, in WiredTiger, it is at document-level. For reads, there is a shared locking mode, while for write there is an exclusive locking mode.


Question: What is the command to list all the indexes in a collection?

Answer: The command is db.collection.getIndexes();

Answer: Text search can be done using text index. Example –


Question: What is the default interval to write updates to the disk?

Answer: The default interval is 60 seconds.

Question: Explain aggregation in MongoDB.

Answer: Aggregation groups values from different documents and performs operations on the data to return a single result. There are 3 ways in which MongoDB performs aggregation –

  • Aggregation pipeline
  • Map-reduce function
  • Single purpose aggregation methods

Question: State the difference between find() and limit() method.

Answer: find() – displays only selected data rather than all the data of a document. For example, if your document has 4 fields but you want to show only one, set the required field as 1 and others as 0.


limit() – limit function limits the number of records fetched. For example, if you have 7 documents but want to display only the first 4 documents in a collection, use limit. Syntax –


Question: How does concurrency affect primary replica set?

Answer: When the collection changes are written to primary, MongoDB writes the same to a special collection in the local database, called the primary’s oplog. Hence, both the collection’s database and local database are locked.

Question: Give an example of insert, delete and update statement in MongoDB.

 _id: ObjectId(7df74fd8902c),
 title: 'All about MongoDB', 
 description: 'Everything you need to know about MongoDB',
 by: 'Author1',
 url: 'http://www.blogsuthor1.com'
Delete Remove a particular document -
 db.books.remove({'title':'All about MongoDB'})

Remove all the documents –

Update To update values of an existing document –
about MongoDB'},{$set:{'title':'MongoDB for dummies'}})

To replace an existing document with a new
one based on ObjectId –

 "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB for dummies",

Question: What have capped collections?

Answer: Capped collections are fixed-size collection and insert and retrieve data based on insertion order. If a collection’s space is full, the oldest records will be overwritten by the new documents in the collection.

Question: Explain the purpose of map-reduce command in Mongo-DB.

Answer: Map-reduce is a method of performing aggregation.

  • Map function emits key-value pair specified.
  • Reduce function combines the key value pair and returns the aggregation result.

Syntax –

 function() {emit(key,value);}, 
 function(key, values) {return aggregatedResult}, { out: collection } 

Question: List some of the data types supported by MongoDB.

Answer: Some data types are Numbers, String, Array, Binary data, Boolean, Date, Regular expressions, ObjectId, etc…

Question: How can applications access real-time data changes in MongoDB?

Answer: Applications can access real-time data changes using Change streams which acts as a subscriber to all the collection operations like insert, delete and update.

Question: What are the commands to create a backup of the data and restore the data?

Answer: Use the command mongodump to create a backup.

Use the command mongorestore [backup_path] to restore the data.

Question: Explain the difference between MongoDB and Cassandra?


Parameters MongoDB Cassandra
Structure of Data Best database to use when there is no clear structure of the data, i.e. data is a mix of structured and unstructured. Works for both unstructured and structured data, especially when the database is expected to grow dynamically.
Data Model Has a rich and expressive data model, which is data-oriented. Any data structure can be easily represented. Traditional model with rows and columns as in a table. Each column is of a specific type, which makes the structure organized.
Number of Master Nodes There is only one master node in the cluster, which controls many slave nodes. There are many master nodes in the cluster.
Secondary Indexes It is easy to index any property because the secondary indexes are first-class constructs. Secondary indexes are limited to single columns and have less flexibility.
Scalability For data to be written in slave nodes, it has to pass through the single master node, so scalability is less. Since there are many master nodes, data is present in multiple nodes. Hence, the scalability is high.
Aggregation Framework Built-in aggregation framework. No built-in aggregation framework.

Question: Can MongoDB Support foreign key constraints?

Answer: No, MongoDB doesn’t support foreign key constraints. Because of the document structure, MongoDB provides flexible ways to define relationships.

Question: What are indexes in MongoDB?

Answer: In MongoDB, indexes help to execute the queries efficiently. Indexes are special data structures that store part of the collection in a form easy to traverse. By default, MongoDB creates a permanent unique index on the _id field when a collection is created. It prevents duplicate documents in the database.

Question: What is a Storage Engine in MongoDB?

Answer: It is a component of the database that manages how data is stored in the memory and disk both. MongoDB provides support for multiple storage engines that helps in better performance for different workloads. The default storage engine is WiredTiger (MongoDB3.2), which is well-suited for most workloads.

Question: Explain the working mechanism Journaling work in MongoDB?

Answer: Journaling is used to recover information after the last checkpoint when MongoDB exits unexpectedly. The storage engine (WiredTiger) creates a journal record for each of the clients that initiated the write operation. If there is an update, a single journal record records the update operation as well as index modifications. Journal records are stored using in-memory buffering. Journal files are stored under the ‘journal’ directory created by MongoDB. Read more details.

Question: Is it possible to configure the cache size for MMAPv1?

Answer: It is not possible to configure the cache size for MMAPv1. MongoDB uses all the free memory automatically through memory-mapped files.

Question: Explain GridFS in MongoDB?

Answer: GridFS stores and retrieves large files like images, audio and video files etc. Although the limit to store a file is 16MB, GridFS can store files with size greater than that. GridFS breaks the file into chunks and stores each chunk as a different document of maximum size 255k. It uses two collections, fs.chunks and fs.files for storing chunks and metadata, respectively.

Question: Is it possible to run multiple Javascript operations in a MongoDB instance?

Answer: Yes, we can run multiple JS operations in a MongoDB instance. Through the mongo shell instance, we can specify the name of the JavaScript file to be run on the server. The file can contain any number of JS operations.

Question: MongoDB is the best NoSQL Database? How and why?

Answer: MongoDB is the most suitable NoSQL database for data analytics as well as web application development. It can help developers and data scientists get insights from the stored data, unlike Hadoop, which requires experts to analyze data.

MongoDB allows for efficient data manipulation and administration capabilities. The support for multiple JavaScript operations and Javascript-based MEAN stack add to the popularity of MongoDB. Sharding for horizontal scaling and aggregation framework to build pipelines made MongoDB fast and efficient and hence the best database to use.

Question: Explain the process of Transaction/Locking in MongoDB?

Answer: MongoDB supports multi-granular locking with read and write locks.

There are three types of locking mechanisms possible in MongoDB:

  • Global level
  • Database level
  • Collection level

The implementation also depends on the individual storage engine. There are four modes of locking:

  • R(shared lock),
  • W(exclusive lock),
  • r(intent shared lock),
  • w(intent exclusive lock).

To read more, visit the official documentation page.

Question: Explain how MongoDB is different from RDBMS?


Parameters RDBMS MongoDB
Definition It is a relational database management system. It is a non-relational DB.
Working Works on relationships between tables that use rows and columns. Document oriented system using documents and fields.
Hierarchical Data Storage Difficult to store hierarchical data. In-built provision for storing hierarchical data
Scalability Vertically scalable. Vertically and horizontally scalable.
Performance Performance increases with an increase in RAM capacity. Performance increases with increase in processors.
Schema Schema has to be pre-decided and designed, changes to the schema are difficult. Dynamic creation and management of schema making the design flexible.
Support for Joins Supports complex joins. No support for joins.
Query Language Uses SQL for querying the database. BSON is used for database querying.
Support for Javascript No support for JavaScript-based clients to query the database. Provision for Javascript-based clients to query the database.

Question: Explain the procedure of starting the MongoDB server/ instance?

Answer: To start a MongoDB instance, follow the steps as below:

  • First, open the command prompt and run mongod.exe.
  • Alternatively, you can move to the path where MongoDB is installed, for example, “C: MongoDB”
  • Navigate to the bin folder, locate the mongod.exe and double click the same to execute it.
  • You can also navigate to the required folder, for example, “C: MongoDB/bin” and type mongo to connect MongoDB through the shell.

Question: How do you refer to the current database in a statement? (by using db)

Answer: Current database is referred to as ‘db’. Example – db.collection.find();

If you are looking for more no-SQL-database-based interview questions, here is a great course that will help you prepare well for MongoDB interviews: MongoDB Interviews Questions and Answers.

Usually, you would be asked many generic programming questions from the data structure and basic programming languages. Here is a great course: Cracking the IT Architect Interview.

People are also reading:

Ramya Shankar

Ramya Shankar

A cheerful, full of life and vibrant person, I hold a lot of dreams that I want to fulfill on my own. My passion for writing started with small diary entries and travel blogs, after which I have moved on to writing well-researched technical content. I find it fascinating to blend thoughts and research and shape them into something beautiful through my writing. View all posts by the Author

Leave a comment

Your email will not be published

Select from the best sales here