MongoDB Interview Questions
Q:- What is MongoDB?
MongoDB is a NoSQL database

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

Q:- In which language MongoDB is written?

MongoDB is written in C++.

Q:- What are collection in MongoDB?

A collection in MongoDB is a group of documents. A collection in MongoDB is very similar to table in a RDBMS.

Q:- What are the capped collection in MongoDB?

A capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. A capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting or deleting the oldest documents in the collection automatically.

Following are the use of cappped collection

  1. Store log information generated by high-volume systems
  2. Cache small amounts of data in a capped collections
db.createCollection("log", { capped : true } );

The above command will create a capped collection - log

Q:- What are the key features of MongoDB?
  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
You may also like - React.js Interview Questions
Q:- What are the different types of NoSQL databases?
  1. Key-Value database
  2. Document based database
  3. Column based database
  4. Graph base database
  1. Key-Value database:

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

    #Example: DynamoDB (Amazon S3) and Redis (In Memory Database), etc.

  2. Document based database:

    It stores data in form of documents (BSON format) and it can contains many different key-value pairs, or key-array pairs, or even nested documents.

    #Example: MongoDB

  3. Column based database:

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

    #Example: Cassandra, HBase, etc

  4. Graph based database

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

    #Example: Neo4J and HyperGraphDB, etc

Q:- Which are the different languages supported by MongoDB?

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

Q:- What is mongod?

mongod is MongoDB server. it start the MongoDB process and run it in the background.

Q:- What is mongos?

The mongos tracks what data is on which shard by caching the metadata from the config servers.

Q:- What is mongo?

mongo is the command-line shell that connects to a specific instance of mongod.

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" : "test1@test.com" }, { "_id" : ObjectId("12bf63500be1d7770c3982b0"), "name" : "Test 2", "email" : "test2@test.com" } //To get only name of users for all the documents, we will use the projection: > db.user.find({}, {"_id": 0, "name": 1}); { "name" : "Test 1" } { "name" : "Test 2" }
Q:- Does the MongoDB have the Schema?

Yes, but it have the dynamic and flexible 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 we can achieve something similar like primary key-foreign key relationship using following methods:

  1. Embedding - A document nested inside another document.
  2. Referencing - It store the _id of an document into another document to refer.
Q:- What is a Namespace?

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 is a special capped collection which stores an ordered history of all logical operations that modify the data stored in your database.

You may also like - JavaScript Interview Questions
Q:- What is Aggregation?
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.
db.collection.aggregate([
 {$project:{}}
 {$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:- What is ObjectId?

ObjectId is a 12-byte BSON type.

ObjectId consists of

  1. 4 bytes represents seconds
  2. 3 bytes machine identifier
  3. 2 bytes process id
  4. 3 bytes counter
Q:- What are Indexes?
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:- Which index is created by default for every collection?

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

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

Synatx:
db.collection.createIndex(keys, options);

#Example:

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

There are following types of Indexes

  1. Single Index
  2. Compound Index
  3. Multikey Index
  4. Geospatial Index
  5. Text Index
  6. Hashed Index
#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.
Note:
  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:- 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:- What is explain() in MongoDB ?
In the Mongo shell, you also can retrieve query plan information through the explain() method:
db.collection.explain();
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

db.serverStatus().wiredTiger;

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:- 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:

db.setProfilingLevel(1);

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 (local.oplog.rs). 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 lies 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 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 to check the number of connections in MongoDB?
db.serverStatus().connections;

//Output
{"current" : CURRENT_CONNECTION_COUNT, "available" : TOTAL_CONNECTION_COUNTS}
//sample output
{"current" : 15, "available" : 15000}

Q:- What are the disadvantages of MongoDB?
  1. Document Size: Max document size is 16 MB.
  2. Transactions: There is no default transaction support.
    RDBMS is best choice for transactions based applications
  3. Joins: No Support for Joins as in RDBMS.
Most frequently asked mongodb interview questions for fresher and experienced.
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?