MongoDB Interview Questions: MongoDB Interview Questions for beginners and professionals. MongoDB is an open source cross-platform document-oriented NoSQL database.
MongoDB Interview Questions
Q:- What do you understand by NoSQL databases?

There is a problem to handle Big data, Unstructured data and Semi Structured data, So NoSQL is answer of all these problems.

NoSQL stands for "Not Only SQL". NoSQL is a type of database that can handle and sort all type of unstructured, messy and complicated data.

Q:- Is MongoDB a NoSQL database?

Yes. MongoDB is a NoSQL database.

Q:- What are the different types of NoSQL databases? Give some example.
NoSQL databases typically fall into one of four categories:
  1. Key-Value Store:

    Every item in the database is stored as an attribute name (or "key") together with its value.

    #Example: Riak, Voldemort, Amazon S3 (Dynamo) and Redis etc.

  2. Document based databases:

    It pairs each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents.

    #Example: MongoDB

  3. Column based Store:

    It stores data together as columns instead of rows and are optimized for queries over large datasets.

    #Example: Cassandra, HBase etc

  4. Graph base databases

    It is used to store information about networks, such as social connections.

    #Example: Neo4J and HyperGraphDB etc

Q:- List most popular NoSQL Database [2020]?

Top 3 are the most popular NoSQL database:

  1. MongoDB
  2. DynamoDB
  3. Cassandra
Q:- Which are the different languages supported by MongoDB?

MongoDB provides official driver support for C/C++, C#, Java, Node, Perl, PHP, Python, Ruby, etc.

Q:- Is MongoDB better than other SQL databases? If yes then how?

Yes, MongoDB is better than other SQL databases in terms of following conditions.

  1. High Performance -
    MongoDB provides high performance data persistence. It supports embedded data models to reduce I/O activity on a database system, as well as indexes for faster queries, and can include keys from embedded documents and arrays
  2. High Availability -
    To provide high quality availability, MongoDb’s replication facility (known as replica sets) provide both automatic failover and data redundancy. A replica set is a group of MongoDB servers that maintain the same data set and provide both redundancy and increased data availability
  3. Automatic Scaling -
    MongoDB provides horizontal scalability as part of its core functionality. Automatic sharding distributes data across a cluster of machines, while replica sets can provide eventually-consistent reads for low-latency deployments
  4. Schema Less -
    JSON data model with dynamic schemas
  5. Semi-Structured/Unstructured -
    Best Suited for Semi-Structured/Unstructured Data as well as Structured data also Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data
  6. Free -
    MongoDB is free and open-source, published under the GNU Affero General Public License
Q:- Why MongoDB is known as best NoSQL database?
MongoDb is the best NoSQL database because, it is:
  1. Document Oriented
  2. Rich Query language
  3. High Performance
  4. Highly Available
  5. Easily Scalable
You may also like - React.js Interview Questions
Q:- What is mongo in MongoDB?
mongo is an interactive JavaScript shell interface to MongoDB

It provides a powerful interface for system administrators as well as a way for developers to test queries and operations directly with the database.

Q:- What is mongod in MongoDB?
  • mongod is the MongoDB database server.
  • The mongod process starts the MongoDB server as a daemon.
  • The MongoDB server manages data requests and formats and manages background operations
Q:- What is mongos in MongoDB?

The routing and load balancing process that acts an interface between an application and a MongoDB sharded cluster.

Q:- What is a Namespace in MongoDB?
The namespace is a combination of the database name and the name of the collection or index.

Example: [database-name].[collection-or-index-name]

Q:- What is oplog in MongoDB?
  • oplog stands for operations log.
  • oplog is a special capped collection which stores an ordered history of all logical operations that modify the data stored in your database.
Q:- What is MongoDB Projection?

MongoDB projection means selecting only the necessary data rather than selecting whole of the data of a document.

//User Collection { "_id" : ObjectId("25bf63380be1d7770c3982af"), "name" : "Test 1", "email" : "[email protected]", "age" : 22 } { "_id" : ObjectId("12bf63500be1d7770c3982b0"), "name" : "Test 2", "email" : "[email protected]", "age" : 22 } { "_id" : ObjectId("34bf63650be1d7770c3982b1"), "name" : "Test 3", "email" : "[email protected]", "age" : 23 } //To get only name of users for all the documents, we will use the Projection like this: > db.user.find({}, {"_id": 0, "name": 1}); { "name" : "Test 1" } { "name" : "Test 2" } { "name" : "Test 3" }
Q:- Does the MongoDB have the Schema?

Yes, MongoDB have the Dynamic Schema so there is no need to define structure to create collections in MongoDB.

Q:- Does MongoDB support primary-key and foreign-key relationship?

No. By Default, MongoDB doesn't support primary key-foreign key relationship.

But, In MonogDB We can achieve primary key-foreign key relationship in following ways:

  1. embedding
  2. referencing

#Example: An address document can be embedded inside user document.

{ _id: "4f7ee46e08403d063ab0b4f9", name: "Harry", addresses: [ { street: "123 Street 1", city: "New Delhi", state: "New Delhi", zip: "12345" }, { street: "Some Other Street", city: "Boston", state: "MA", zip: "12345" } ] }
You may also like - JavaScript Interview Questions
Q:- What is Aggregation in MongoDB?
Aggregation operations process data records and return computed results.

Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.

MongoDB provides three ways to perform aggregation:

  1. aggregation pipeline
  2. map-reduce function.
  3. single purpose aggregation methods.
Aggregation Pipeline - A series of operations in an aggregation process.

The MongoDB aggregation pipeline consists of stages.

  • Each and evenry stage transforms the documents as they pass through the pipeline.
  • Pipeline stages do not need to produce output documents for each and every input documents.
  • MongoDB provides the db.collection.aggregate() method.
 {$match: {}},
 {$group: {}},
 {$sort: {}}

Note- Pipeline stages can appear multiple times in the pipeline.

There are following possible stages in aggregation pipeline.

  1. $project − It is used to select only specific fields from a collection.

  2. $match − It is used to filter out the document based on some conditions.

  3. $group − It is used to group the documents.

  4. $sort − It is used to sorts the documents.

  5. $skip − It is used to skip documents.

  6. $limit − It is used to limit the documents.

  7. $unwind − It is used to unwind documents.

Q:- Does MongoDB need a lot of RAM?

No It can also be run on small amount of RAM because it dynamically allocates and de-allocates RAM according to the requirement of the processes.

Q:- Explain the structure of ObjectID in MongoDB.

ObjectId is a 12-byte BSON type.

ObjectID consists of

  1. 4 bytes value representing seconds
  2. 3 byte machine identifier
  3. 2 byte process id
  4. 3 byte counter
Q:- Is it true that MongoDB uses BSON to represent document structure?
Q:- What are Indexes in MongoDB?
In MongoDB, Indexes are used to execute query efficiently.

It imporoves the search performance but the same time if you will create too many unnecessary index then it may slow down write operations.

Q:- By default, which index is created by MongoDB for every collection?

By default, the _id collection is created for every collection by MongoDB.

To create an index in the Mongo Shell, use db.collection.createIndex().

db.collection.createIndex(keys, options);


db.user.createIndex( { name: -1 } );

There are following types of Indexes in MongoDB

  1. Single Field
  2. Compound Index
  3. Multikey Index
  4. Geospatial Index
  5. Text Indexes
  6. Hashed Indexes
#Example : Create an Index on a Multiple Fields

The following example creates a compound index on the username field (in ascending order) and the city field (in descending order.)

db.user.createIndex( { "username": 1 , "city": -1 } );
NOTE: Order of Index

The order of an index is important for supporting sort() operations using the index.

The createIndex() method has the behaviors described here.
  1. To add or change index options you must drop the index using the dropIndex() method and issue another createIndex() operation with the new options.
  2. If you create an index with one set of options, and then issue the createIndex() method with the same index fields and different options without first dropping the index, createIndex() will not rebuild the existing index with the new options.
  3. If you call multiple createIndex() methods with the same index specification at the same time, only the first operation will succeed, all other operations will have no effect.
  4. Non-background indexing operations will block all other operations on a database.
  5. MongoDB will not create an index on a collection if the index entry for an existing document exceeds the Maximum Index Key Length.
  1. Use db.collection.createIndex() rather than db.collection.ensureIndex() to create indexes.
  2. Use db.collection.getIndexes() to view the specifications of existing indexes for a collection.

For more details - please visit MongoDB Official Website

Q:- In which language MongoDB is written?

MongoDB is written in C++.

Q:- How to do Transaction/locking in MongoDB?
MonogDB doesn't supports Transaction
  1. MongoDB doesn't use traditional locking or complex transaction with Commit and Rollback.
  2. MongoDB is designed to be light weighted, fast and predictable to its performance. It keeps transaction support simple to enhance performance.
Q:- Why 32 bit version of MongoDB are not preferred?

Because MongoDB uses memory mapped files so when you run a 32-bit build of MongoDB, the total storage size of server is 2 GB. But when you run a 64-bit build of MongoDB, this provides virtually unlimited storage size. So 64-bit is preferred over 32-bit.

Q:- What is explain() in MongoDB ?
In the Mongo shell, you also can retrieve query plan information through the explain() method:
You may also like - MySQL Interview Questions
Q:- Can one MongoDB operation lock more than one databases? If yes, how?
Yes. Operations like copyDatabase(), repairDatabase(), etc. can lock more than one databases involved.
Q:- What is a Storage Engine in MongoDB?

A storage engine is the part of a database that is responsible for managing how data is stored on disk.

#Example, one storage engine might offer better performance for read-heavy workloads, and another might support a higher-throughput for write operations.

You can find the storage engine currently being used.

db.serverStatus().storageEngine; //Output: { "name" : "wiredTiger" }

Once it is confirmed that wiredTiger is being used then type


to get all the configuration details of wiredTiger.

Q:- Which are the two storage engines used by MongoDB?
MongoDB uses:
  1. MMAPv1
  2. WiredTiger
Q:- Does MongoDB provide a facility to do text searches? How?
Yes, MongoDB supports creating text indexes to support text search inside string content. This was a new feature which can introduced in version 2.6.
Q:- What is 32-bit nuances?
There is an extra memory mapped file activity with journaling. This will further constrain the limited DB size of 32-bit builds. For now, journaling by default is disabled on 32-bit systems.
Q:- Why does Profiler used in MongoDB?

A tool that, when enabled, keeps a record on all long-running operations in a database's system.profile collection.

The profiler is most often used to diagnose slow queries.

Enable profiling:


Now let it run for a while. It collects the slow queries ( > 100ms) into a capped collections, so queries go in and if it's full, old queries go out, so don't be surprised that it's a moving target

Find the most recent slow query:

db.system.profile.find().sort({$natural: -1}).limit(1)
Q:- What is map-reduce in MongoDB?

A data processing and aggregation paradigm consisting of a "map" phase that selects data and a "reduce" phase that transforms the data. In MongoDB, you can run arbitrary aggregations over data using map-reduce.

For map-reduce operations, MongoDB provides the mapReduce database command.

Q:- Explain the Replication in MongoDB?
  • Replication is the process of synchronizing data across multiple servers.
  • Replication provides redundancy and increases data availability with multiple copies of data on different database servers
  • Replication also allows you to recover from hardware failure and service interruptions.

Replication in more details

Q:- When an object attribute is removed, is it deleted from the store?
Yes, you can remove the attribute and then re-save() the object.
Q:- Are null values allowed in MongoDB?
Yes, but only for the members of an object. A null cannot be added to the database collection as it isn't an object. But {} can be added.
Q:- Does an update fsync to disk immediately?
No. Writes to disk are lazy by default. A write may only hit the disk a couple of seconds later. For example, if the database receives a thousand increments to an object within one second, it will only be flushed to disk once. (Note: fsync options are available both at the command line and via getLastError_old.)
Q:- Why are data files so large?
MongoDB does aggressive preallocation of reserved space to avoid file system fragmentation.
Q:- How long does replica set failover take?
It may take 10-30 seconds for the primary to be declared down by the other members and a new primary to be elected. During this window of time, the cluster is down for primary operations i.e writes and strong consistent reads. However, eventually, consistent queries may be executed to secondaries at any time (in slaveOk mode), including during this window.
Q:- What’s a Master or Primary?
This is a node/member which is currently the primary and processes all writes for the replica set. During a failover event in a replica set, a different member can become primary.
Q:- What’s a Secondary or Slave?
A secondary is a node/member which applies operations from the current primary. This is done by tailing the replication oplog ( Replication from primary to secondary is asynchronous, however, the secondary will try to stay as close to current as possible (often this is just a few milliseconds on a LAN).
Q:- Is it required to call ‘getLastError’ to make a write durable?
No. If ‘getLastError’ (aka ‘Safe Mode’) is not called, the server does exactly behave the way as if it has been called. The ‘getLastError’ call simply allows one to get a confirmation that the write operation was successfully committed. Of course, often you will want that confirmation, but the safety of the write and its durability is independent.
Q:- What is sharding in MongoDB?

A database architecture that partitions data by key ranges and distributes the data among two or more database instances. Sharding enables horizontal scaling.

  • In MongoDB, Sharding is a procedure of storing data records across multiple machines.
  • It is a MongoDB approach to meet the demands of data growth.
  • It creates horizontal partition of data in a database or search engine.
  • Each partition is referred as shard or database shard.
Q:- Should you start out with Sharded or with a Non-Sharded MongoDB environment?
We suggest starting with Non-Sharded for simplicity and quick startup unless your initial data set will not fit on single servers. Upgrading to Sharded from Non-sharded is easy and seamless, so there is not a lot of advantage in setting up Sharding before your data set is large.
Q:- How does Sharding work with replication?
Each Shard is a logical collection of partitioned data. The shard could consist of a single server or a cluster of replicas. Using a replica set for each Shard is highly recommended.
Q:- When will data be on more than one Shard?
MongoDB Sharding is range-based. So all the objects in a collection lie into chunks. Only when there is more than 1 chunk there is an option for multiple Shards to get data. Right now, the default chunk size is 64mb, so you need at least 64mb for migration.
Q:- What happens when a document is updated on a chunk that is being migrated?
The update will go through immediately on the old Shard and then the change will be replicated to the new Shard before ownership transfers.
Q:- What happens when a Shard is down or slow when querying?
If a Shard is down, the query will return an error unless the ‘Partial’ query options is set. If a shard is responding slowly, Mongos will wait for it.
Q:- How do I view connections in MongoDB OR How to Check the current number of connections in MongoDB?
db.serverStatus().connections; //Output {"current" : CURRENT_CONNECTION_COUNT, "available" : TOTAL_CONNECTION_COUNTS}
Q:- What are the best features OR advantages of Mongodb?
  1. Document-oriented
  2. High performance
  3. High availability
  4. Easy scalability
  5. Rich-query language
Q:- What are the disadvantages of MongoDB?
  1. Document Size:
    Max document size is 16 MB.
  2. Transactions:
    There is no default transaction support; you need to handle this yourself. So for Transactions based Applications RDBMS is best choice.
  3. Joins:
    In MongoDB, it's difficult to represent relationships between data so you end up doing that manually by creating another table to represent the relationship between rows in two or more tables.
Following are some other useful questions related to MongoDB.
Is MongoDB Support Relationship? Is MongoDB Support Replication?
In MongoDB, What is a namespace? In which language MongoDB is written?
Do MongoDB databases have tables and schemas? What types of languages use to work with MongoDB?
Does MongoDB support SQL Server? Does MongoDB support ACID Transactions?
What is the difference between MongoDB and CouchDB? What are different between MongoDB and Sql Server databases?
How MongoDB is better than other SQL Server databases? What are difference between SQL Server and NoSQL Database?
Now days, what makes MongoDB best? How to create primary/foreign key relationships in MongoDB?
What are 32-bit nuances? Can MongoDB used for Cache Management?
Why does Profiler use in MongoDB? In MongoDB, What is a covered query?
In MongoDB, What is Aggregation? In MongoDB, What is Sharding?
Why MongoDB data files so large (in size)? In MongoDB, What is GridFS?
Is MongoDB support null values? In MongoDB, How do I do transactions/locking?
In MongoDB, What is the Master or primary key? In MongoDB, What is secondary or slave?
In MongoDB, How does sharding work with replication? In MongoDB, Can I remove old files in the move Chunk directory?
How can I see the connections which you used in mongos? What are the limitations of MongoDB?
In MongoDB, What is Replication Factor? In MongoDB, What is dynamic Schema?
In MongoDB, What is BSON and how can restore this file? In MongoDB, How can you take database backup?
In MongoDB, What is Replication? In MongoDB, What is the role of 8 Analysers?
In MongoDB, if user removed to object attribute that attribute is deleted from the storage layer? If yes, then how? In MongoDB, Whether use to safe backup log feature?
In MongoDB, How to update operations immediately sync to disk? In MongoDB, How to perform transactions/lock?
In MongoDB, How sharding and replication work together? In MongoDB, How do you configure to cache size for MMAPv1?
Does MongoDB handle application level caching? Why MongoDB logging so many “Connection Accepted” events?
Does MongoDB run on Amazon EBS? In MongoDB, How is Query injection and how to handle it?
How can you enter multi line operations in the mongo shell? How can you access different databases temporarily?
Is mongo shell supported to tab completion? How can you customize to mongo shell prompt?
Can you edit long shell operations with an external text editor? What types of locking use in MongoDB?
How do you see the locking status in mongo instances? Can you perform read/write operation for ever yield lock?
In MongoDB, Which operations lock the database? In MongoDB, Which commands you use to lock the database?
In MongoDB, Can you lock more than one database at the same time? How to create Index after every query insert?
How to know, what indexes exist in a collection? How to determine the size of an index?
What happen when an index does not fit into RAM?