DEV Community

Cover image for Top 30 MongoDB Interview Questions and Answers
Dhiman_aman
Dhiman_aman

Posted on

Top 30 MongoDB Interview Questions and Answers

MongoDB Interview Questions and Answers: An Overview MongoDB is a document-oriented database. MongoDB is a powerful, scalable, and much more flexible database that provides high performance in case of a large volume of data and also leads to a NoSQL Database. MongoDB stores the data in a BSON (Binary JavaScript Object Notation) format.

1. What is MongoDB?
Mongo-DB is a document database with high performance, high availability, and easy scalability. It is an open-source NoSQL database written in C++ language.

2. What is NoSQL Database?

NoSQL stands for Not Only SQL. NoSQL is a category of Database Management System (DBMS) that maintains all the rules of traditional RDBMS systems. It also does not use the conventional SQL syntaxes to fetch the data from the database. This type of database system is typically used in case of a very large volume of data. Some of the well-known NoSQL database systems are – Cassandra, BigTable, DynamoDB, MongoDB, etc.

3. What are the types of NoSQL databases?
There are four types of NoSQL Database available:

Document Database
This type of NoSQL database is always based on a Document-Oriented approach to store data. The main objective of the Document Database is to store all data of a single entity as a document and all documents can be stored as Collections. Some examples of Document Database – are MongoDB, CosmosDB, CouchDB, PostgreSQL, etc.

Key-Value Database
This type of database stores data in a schema-less way since key-value storage is the simplest way of storing data. A key can be a point to any type of data, like an object, string, or any other type of data. The main advantages of these databases are easy to implement and add data into. Example – Redis, DynamoDB, etc.

Column Store Database
These types of databases store data in columns within a keyspace. The key space is always defined by a unique name, value, and timestamp. Example – Cassandra, BigTable, HBase, Vertica, HyperTable.

Graph Store Database
These types of databases are mainly designed for data that can be easily represented as graph data. This means that data are interconnected with an undetermined number of data relations between them like family and social relations etc. Example – AllegroDB, GraphDB, OrientDB, Titan.

4. What are the advantages of MongoDB?
The main advantages of MongoDB are:

  • It can deal with a high volume of data.
  • It supports cross-platform
  • It provides High Performance
  • It is easily scalable
  • It does not require any complex joins to retrieve data
  • It supports both types of scaling – Horizontal & Vertical
  • It is available on any cloud-based environment like Azure, AWS, etc

5. What are Documents?
The document is the heart of MongoDB. In simple words, the document is an ordered set of keys with associated values. It is similar to the table rows in the RDBMS systems. It always maintains a dynamic scheme so that it does not require any predefined structure or fields.

6. What is Collection?
A collection in MongoDB is a group of documents. If a document is the MongoDB analog of a row in an RDBMS, then a collection can be thought of as the analog to a table.
Documents within a single collection can have any number of different “shapes.”, i.e. collections have dynamic schemas.
Syntax
db.createCollection(name,options)

7. What are Dynamic Schemas?
In MongoDB, Collections always have dynamic Schemas. Dynamic Schemas means the documents within a single collection may contain different types of structure or shapes. For example, both the below documents can be stored in a single collection:
{"message" : "Hello World"}
{"id" : 10, "description" : "India"}

8. What is Mongo Shell?
MongoDB Shell is a JavaScript shell that allows us to interact with MongoDB instances using the command line. It is very useful to perform any administrative work along with any other data operations-related commands. Mongo Shell is automatically installed when we install MongoDB on our computers.

9. List out some features of MongoDB.
Indexing: It supports generic secondary indexes and provides unique, compound, geospatial, and full-text indexing capabilities as well.

Aggregation: It provides an aggregation framework based on the concept of data processing pipelines.

Special collection and index types: It supports time-to-live (TTL) collections for data that should expire at a certain time

File storage: It supports an easy-to-use protocol for storing large files and file metadata.

Sharding: Sharding is the process of splitting data up across machines.

10. Where can we use MongoDB?
MongoDB can be used in the following areas:

  • Content Management System
  • Mobile Apps where data volume is very large and requires high readability of data.
  • Data Management
  • Big Data

11. Which languages does MongoDB support?
Several languages are supported by MongoDB like

  • C++
  • C
  • C#
  • Java
  • Node.Js
  • Perl
  • PHP etc.

12. What data types are supported by MongoDB?
MongoDB supports a wide range of data types in documents. Below are the available data types in the MongoDB.
Data Types Descriptions

  • String It is the most commonly used data type. A string must be UTF-8 valid in MongoDB
  • Integer It is used to store numeric values. It may be either 32-bit or 64-bit.
  • Boolean It is used to store Boolean data types. It's valued either true or false.
  • Double It is used to store floating point values.
  • Arrays This data type is used to store a list or multiple values in a single key
  • Objects This data type is used to store embedded data
  • Null It is used to store null data.
  • Date This data type is used to store the current date or time value in Unix time format.

13. What is ObjectId in MongoDB?
Each document stored in MongoDB must contain an “_id” key. The default value type of the “_id” is ObjectId. In a single collection, every document always contains a unique value of the “_id” field, so that every document can be identified easily. ObjectId always uses 12 bytes of storage. It always represents 24 hexadecimal digit string values.
Objectld is composed of:
Timestamp
Client machine ID
Client process ID
3-byte incremented counter

14. How to add data in MongoDB??
The basic method for adding data to MongoDB is “inserts”. To insert a single document, use the collection’s insertOne method:

db.books.insertOne({"title" : "ScholarHat"})

For inserting multiple documents into a collection, use the method insertMany. This method enables passing an array of documents to the database.

15. What is Capped Collection in MongoDB?
In MongoDB, Capped collections are fixed-size collections, and insert and retrieve data based on the insertion order. If a collection’s space is full, the oldest records will be overwritten by the new documents in the collection. So, to create a capped collection, the command will be –

db.createCollection(“CollectionName”, {“capped”:true, “size” : 100000})

16. What is the Full-Text Index?
Full-text indexing is a search engine feature that enables you to perform text searches on a collection of documents within a database. Unlike traditional databases that search through text using the ‘LIKE’ query pattern match, full-text search engines tokenize the text in documents and build an index to allow very fast text search capabilities.

Full-Text Index is one of the special types of Index in MongoDB for searching text within the documents. However, this type of indexing is expensive for use concerns. So, creating a full-text index on a busy collection can overload the MongoDB Server. So it is always recommended to use this type of index in an offline mode. To create a full-text index, the command is –

db.<CollectionName>.ensureIndex({“name” : “text”})

17. What is GridFS?
Since in MongoDB, every document size limit is 16 MB. So, if we want to insert any large binary data file, we need to use GridFS. GridFS is a mechanism through which we can store any type of large file data like an audio file, video file image, etc. It is just like a file system to store these large files and also, its related data stored in the MongoDB collection.

18. What is the purpose of using $group?
$group syntax is used to bundle or group the documents of a collection based on one or more fields. So, if we want to group the data that depends on one or more than one field, we need to pass those fields' name within the group method to create a group key and normally the group key name is “_id”.

{"$group" : {"_id" : {"state" : "$state", "city" : "$city"}}}

We can use any type of arithmetic operator with the group command as below –
db.sales.aggregate(
{
"$group" : {
"_id" : "$country",
"totalRevenue" : {"$average" : "$revenue"},
"numSales" : {"$sum" : 1}
}
})

19. What is Replication?
Replication is the process that is responsible for keeping identical copies of our data in multiple servers and is always a recommended process for any type of production server. Replication always keeps our database safe even if the database server crashes or data is corrupted. With the additional copies, MongoDB maintains the copy of the data for disaster recovery, reporting, or backup purposes.

20. Why Replication is required in MongoDB?
In MongoDB, replication is required for the following reasons –
To provide always the availability of data
To secure our application data
Recover the data from any type of disaster recovery
In the Replication process, no downtime requires maintenance like backup, index rebuilds, etc.
Replication can provide us with a read scaling means it will provide us with a copy of data only for real purposes.

21. Explain the replica set.
It is a group of Mongo instances that maintains the same dataset. Replica sets provide redundancy and high availability and are the basis for all production deployments.

22. What is the use of the map-reduce command?
Map-reduce is a way to perform aggregation.
The Map function emits the key-value pair specified.
The Reduce function combines the key-value pair and returns the aggregation result.
Syntax
db.collection.mapReduce(
function() {
emit(key,value);
}, function(key, values) {return aggregatedResult}, { out: collection })

23. How to delete a Document in MongoDB?
The CRUD API in MongoDB provides deleteOne and deleteMany for this purpose. Both these methods take a filter document as their first parameter. The filter specifies a set of criteria to match against in removing documents.
Example
db.list.deleteOne({"_id" : 9})

24. Describe the process of Sharding.
Sharding is the process of splitting data up across machines. In other words, it is called “partitioning”. We can store more data and handle more load without requiring larger or more powerful machines, by putting a subset of data on each machine.

In the given figure, RS0 and RS1 are shards. MongoDB’s sharding allows you to create a cluster of many machines (shards) and break up a collection across them, putting a subset of data on each shard. This allows your application to grow beyond the resource limits of a standalone server or replica set.

25. What is the use of the pretty() method?
The pretty() method is used to show the results in a formatted way.
Example
Open your Mongo shell and create one collection with a few documents like the below:
db.list.insertMany([
{_id : 1,name : "ScholarHat",age : 2,"employees" : { "JK" : 1, "SD" : 2 }},
{_id : 2,name : "DotNetTricks",age : 10,"employees" : { "SK" : 3, "PC" : 4 }}
])

db.list.find()

It will insert these two documents into the collection list. Now, call db.list.find() to print out all documents in it.

Output :{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }

`{ "_id" : 1, "name" : "ScholarHat", "age" : 2, "employees" : { "JK" : 1, "SD" : 2 } }

{ "_id" : 2, "name" : "DotNetTricks", "age" : 10, "employees" : { "SK" : 3, "PC" : 4 } }`

If the data size is too big, it becomes difficult to read. If you call the pretty() method, the result will be like this:

Output : { "acknowledged" : true, "insertedIds" : [ 1, 2 ] }

{
"_id" : 1,
"name" : "ScholarHat",
"age" : 2,
"employees" : {
"JK" : 1,
"SD" : 2
}
}
{
"_id" : 2,
"name" : "DotNetTricks",
"age" : 10,
"employees" : {
"SK" : 3,
"PC" : 4
}
}

26. Explain the Replication Architecture in MongoDB.
In the above diagram, the PRIMARY database is the only active replica set member that receives write operations from database clients. The PRIMARY database saves data changes in the Oplog. Changes saved in the Oplog are sequential—i.e., saved in the order that they are received and executed.

The SECONDARY database is querying the PRIMARY database for new changes in the Oplog. If there are any changes, then Oplog entries are copied from PRIMARY to SECONDARY as soon as they are created on the PRIMARY node.

Then, the SECONDARY database applies changes from the Oplog to its data files. Oplog entries are applied in the same order they were inserted in the log. As a result, data files on SECONDARY are kept in sync with changes on PRIMARY.

Usually, SECONDARY databases copy data changes directly from PRIMARY. Sometimes a SECONDARY database can replicate data from another SECONDARY. This type of replication is called Chained Replication because it is a two-step replication process.

27. What is Aggregation in MongoDB?
In MongoDB, aggregations are operations that process data records and return computed results. There are three ways to perform aggregation in MongoDB:
Aggregation pipeline
Map-reduce function
Single-purpose aggregation methods

28. Differentiate MongoDB and RDBMS Parameters
RDBMS MongoDB
Definition :
It is a relational database management system. It is a non-relational database management system Working
Works on relationships between tables that use rows and columns A 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 an 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

29. Is it possible to run multiple Javascript operations in a MongoDB instance?
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.

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

Top comments (0)