35 Frequently Asked MongoDB Interview Questions And Answers

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This tutorial provides frequently asked MongoDB interview questions and answers with explanations to help you prepare for the MongoDB interview:

By invent of various technologies like Java, ASP.net, web-based applications were developed for e-commerce, healthcare for online purchase, research, and innovations. It was difficult to store unstructured data – like content in email and SMS, chat, media files, posts, and blogs in relational databases.

MongoDB is a NoSQL database used for accessing and storing unstructured data in documents.

Documents are JSON objects that store data in key-value pairs instead of tables as in relational databases. The collection contains one or multiple such documents.

Understanding MongoDB

MongoDB interview questions

MongoDB is NoSQL database similar to CouchDB, Cassandra and offers advantages over relational databases in processing and managing unstructured data. Such data is essential in Search Engine Optimization, Internet of Things, AI, and Healthcare.

MongoDB is used by various organizations like Barclays, Verizon, Royal Bank of Scotland, KPMG, Vivint, ThermoFisher Scientific, SAP, Cisco, Adobe, and AstraZeneca.

Most Frequently Asked Interview Questions

Q #1) Explain the importance and benefits of using a document-based database.

Answer: Document database such as MongoDB is collection oriented, schema free, open-source distributed database. Documents are Binary JSON format that store data internally as key-value pairs.

Following are benefits of document database:

  • Stores dynamic data in unstructured, semi-structured, and structured formats.
  • Creates and stores for analysis persisted view from the base document.
  • Able to process and store large data sets.
  • No restriction on structure or format for storing data.
  • Objects can be stored in documents and serialized without enforcing any relational integrity.
  • No foreign-key relationship.
  • Supports JSON, XML for documents.

Q #2) Describe some real-world examples of using unstructured data.

Answer: Examples include:

  • Image recognition software can be utilized in classifying mammograms as potentially cancerous glands, genomics – field of biology focusing on structure, function, and evolution of genomes (DNA) to understand disease markers.
  • Text analytics using NLP (Natural Language Processing) or machine learning to extract people, places or things, themes or sentiment from chat, social media posts, comments, and online surveys for building predictive analysis.
  • Analysis of medical case studies and expert opinions on a variety of diseases and treatments can help build healthcare applications using the data from medical reports and lines of treatment as reference data.

Q #3) List the non-tabular databases.

Answer: Non-tabular databases fall into the following three categories:

  • Document databases: MongoDB, Azure CosmosDB, Apache CouchDB.
  • Graph databases: Neo4j, Amazon Neptune, RedisGraph, ArangoDB, DGraph.
  • Key-value databases: Redis, Amazon DynamoDB (key-value), Apache HBase, Cassandra (wide-column).

non-tabular databases

Q #4) List MongoDB cloud products.

Answer: Cloud-based MongoDB products are:

  • MongoDB Realm
  • MongoDB Atlas
  • MongoDB Atlas Search
  • MongoDB Atlas Data Lake
  • MongoDB Charts
  • MongoDB Cloud Manager
  • MongoDB Ops Manager

Q #5) List advantages of MongoDB.

Answer: MongoDB offers the following advantages over other database systems:

  • Stores data records in documents grouped in collections.
  • MongoDB 3.4 supports read-only views whereas MongoDB 4.2 supports on-demand materialized views.
  • MongoDB offers expressive query language, aggregation, and indexing to find, filter, and access data as per business needs in very less time.
  • MongoDB uses shard keys to partition data into chunks and distributes evenly across shards and scales the data
  • MongoDB keeps multiple copies of data thereby offering high availability
  • MongoDB supports programming languages like Erlang, Go, Scala, Java, Node.js, PHP, Python, Ruby, Perl, C, C++ and C#.

Q #6) List the scenarios where MongoDB can be used for storage.

Answer: MongoDB database is suitable for applications that require low latency, high availability, unstructured, semi-structured and polymorphic data storage, and in applications where large scalability and multi-data centers deployment are needed.

MongoDB use-cases include content management, real-time personalization, the application that has a single view across multiple systems. MongoDB is not suitable for complex transactions like double-entry bookkeeping and scan-based applications accessing large data subsets.

Q #7) What is sharding in MongoDB?

Answer: A procedure to store data records across multiple server machines, in order to meet data growth is known as sharding in MongoDB. It creates horizontal partition referred as shard or database shard of data in database or search engine.

Q #8) List the advantages of MongoDB over RDBMS.

Answer: The comparison between RDBMS and the advantage of using MongoDB over RDBMS is explained in the table below:

Relational Database SystemsMongoDB
Database Schema is logical representation of integrity constraints that governs and shows how entities such as tables, views, stored procedure etc. are related to each other. There is logical and physical database schemaIt is document database where collection holds multiple documents where number of field, size and content can differ from one document to another. By default it does not have schema, but dynamic schema can be created
There are 5 types of Joins used in RDBMS - Self join, Inner Join, Left join, Right Join, Full JoinNo complex joins used in MongoDB
Performance tuning is complex and need skilled admin person Performance tuning is very easy compared to RDBMS
Large data are stored in files which are converted into blob data type to store in RDBMS Supports storage of large files, horizontal and hierarchical storage
RDBMS are RAM driven, its performance of finding / retrieving results will be greatly affected with large data storageIt allows third party pluggable storage engine API
RDBMS is not suitable for big data that are not horizontally scalableMongoDB offers scalability using horizontal Sharding – data partition across various servers

Q #9) Describe Mongo shell.

Answer: Mongo shell is the JavaScript shell that helps to configure, query, connect and work with the MongoDB database from the command line. It is an open-source standalone utility under Apache 2 license.

=>> Click here to download.

Q #10) Describe documents in MongoDB.

Answer: MongoDB stores data records as documents as field-value pairs similar to JSON objects.

Structure of JSON like documents:

{
consumer_id:”A9871201″,
consumer_name:”emily”,
mobile_no:”(905)789-9870″,
emai_address:”emily1987@gmail.com”
consumer_since:”01-12-2010″
loyalty_point:8000
}

  • Documents correspond to native data types such as Integer, floating-point, Boolean, characters & strings, etc. in many programming languages.
  • Dynamic Schema offers support to polymorphism.
  • Embedded documents and arrays are used instead of Joins.

Q #11) List the checkpoints while creating dynamic schema in MongoDB.

Answer: The checkpoints or considerations while creating dynamic schema are:

  • Schema should be created based on end-user needs.
  • Multiple objects can be combined into a single document.
  • Joins should be done while writing and not while reading.
  • Schema should be optimized to common use cases.
  • Complex aggregation can be performed in schema.

Q #12) Describe collections in MongoDB.

Answer: MongoDB stores documents (data records) in Collections similar to tables in the relational databases. Initially, when collection does not exist, MongoDB will create Collection when the first data record is stored in the collection.

The commands used are:

db.newCollection1.insertOne ( { x: 1 } )

db.newCollection2.createIndex ( { y: 1 })

Both above operations create respective Collections if they do not exist.

Q #13) What is the namespace in MongoDB?

Answer: It is a combination of the database name and collection name, i.e. concatenation of database name and collection name, like [database-name].[collection or index name].

MongoDB stores Binary JSON (BSON) objects.

Q #14) How data is added in MongoDB?

Answer: insertOne() method is used to insert document (data record) into a collection. In case collection does not exist, insertOne() method creates collection.

Syntax for adding data record in collection is as below:

db.collection.insertOne (
<document>,
{ writeConcern: <document> })

Where, document is data record inserted into collection, writeConcern is optional.

The above command will return a Boolean acknowledged as true, if it is run with write concern or false if write concern was disabled.

Q #15) Explain how the information gets updated in MongoDB?

Answer: Document in MongoDB get updated using update command.

db.Customer.update (
{“Customerid” : 201 },
{$set: { “CustomerName” : “ANZ Grindlays Bank” }} );

Q #16) Explain how document is deleted in MongoDB?

Answer: Documents can be removed from MongoDB collection using db.collection.remove() method.

Syntax:

db.Orders.remove ( { order_id : 30211 } )

If deletion operation is successful, the output will be showing 1 document was removed.

writeResult ({ “nRemoved” : 1})

Q #17) List the queries performed in MongoDB.

Answer: The below table will explain the queries, syntax, and description of the query used in managing data in MongoDB.

QuerySyntaxDescription
find() methoddb.mycollection.find({birth_date: { $gt: new Date(‘1985-07-15’)Returns the documents where birth_date is greater than new Date(‘1985-01-15’)
find() , AND conditiondb.mycollection.find({name : {first: “John”, last:”Cena”}})Returns documents where embedded document name {first: “John”, last:”Cena”}
Find(), OR conditiondb.musicians.find( {$or : [ {instrument : “piano” },
{ born : 1940 }] })
Returns documents where musicians playing instrument as piano or were born in the year 1940
$in operatordb.skillset.find ( {“skill_set”: { $in : [ “Java”, “REST”] } } )$in operator returns list of values in the query, in this example skill_set are Java and REST

Q #18) List the difference between save and insert commands in MongoDB.

Answer: Following is the difference between save and insert commands:

ScenarioDescription
Save command used with _idThe document found with matching _id is replaced with new document
Save command without _idInserts new document created
insert command with _id Gives E11000 duplicate key error that lists collection, index and duplicate key
insert command without _idInserts new document created

Without _id insert and save command act similarly.

db.collection_name.save({ it_asset:”Quick Heal AntiVirus Pro”, product_key: “F58012W076186540F0” })

db.collection_name.insert({ it_asset:”Quick Heal AntiVirus Pro”, product_key: “F58012W076186540F0” })

Q #19) Explain various data types in MongoDB.

Answer: The data types used in MongoDB are listed below:

  • String: UTF-8 compliant strings values can be stored in the MongoDB database.
  • Integer: Saves numerical values of 32 or 64 bit based on the server.
  • Double: Floating point values.
  • Boolean: Stores Boolean values such as True/False.
  • Null: Null value can be stored.
  • Array: Multiple values in one key can be stored.
  • Object: Embedded documents are stored as Object.
  • Object ID: Document ID can be stored in object Id data type.
  • Binary Data: Data stored in 0s and 1s format, e.g. byte (unit for storing electronic memory).

Date, regular expression, symbol, code, and timestamp are few additional data types MongoDB supports.

Q #20) Describe Object Id in MongoDB?

Answer: Every document in MongoDB collection has a “_id” field acting as the primary key that uniquely identifies the document known as the Object ID. Object ID is BSON (binary JSON) data type field of 12 bytes, starting with 4 bytes that represent UNIX timestamp of the document, followed by 3 bytes of machine id MongoDB server running on.

Next 2 bytes are of process id and finally last 3 bytes are counter used for incrementing Object Id.

Structure of Object ID:

Object Id in MongoDB

Q #21) Describe replica set in MongoDB.

Answer: Group of MongoDB instances hosting the same data set are known as replica. Set of replica consists of one primary node and another secondary node. The primary node accepts write and is known as master nodes. Secondary nodes are read-only nodes replicating from primary known as slave nodes.

Q #22) Describe storage engines in MongoDB.

Answer: Storage engine is a database component that manages how data is stored, both in memory and on disk. In order to perform better for specific workloads, MongoDB supports multiple storage engines. In-Memory Storage Engine and WiredTiger are two storage engines used by MongoDB.

  • WiredTiger storage engine provides features such as document level concurrency model, checkpoint, and compression.
  • In-memory storage retains documents in memory instead of on-disk, for more predictable data latencies.

Q #23) Explain the function of index in MongoDB.

Answer: Indexes store a small portion of the data set in easy to traverse (move back and forth) form. The value of a specific field or set of fields is stored by index in the order of value of the field specified by index.

In absence of an Index, MongoDB has to scan each document in the collection to fetch those documents that match the query statement. _id also known as object Id is the default index MongoDB creates for every collection.

The syntax to create index in MongoDB is as follows:

db.collection_name.createIndex({ Key : 1}), where Key is the field name on which index is created and 1 is for ascending order. For descending order -1 is used.

Q #24) List the difference between MongoDB and CouchDB.

Answer: Both MongoDB and CouchDB are document based databases. The difference between them are listed below:

MongoDBCouchDB
MongoDB follows document based data model and data is stored in BSON (binary JSON) formatCouchDB also follows document based data model but stores data in JSON format
MongoDB uses simple socket based, request-response style protocol based on TCP as its transport layer protocol It uses HTTP/REST based protocol
Database contains collections that contains documents (data records) Data is stored in documents in database
It follows object based query language and Map/Reduce Java scriptIt follows Map/Reduce query methods
It supports Master-slave replicationIt supports master-master replication with built- in custom conflict resolution feature.
It is written in C++ language.It is written in Erlang language
It is highly available and strongly consistentIt favours consistency

Q #25) List the differences between MongoDB and Cassandra.

Answer: MongoDB is document based whereas Cassandra is a wide column-based database. The difference between them are as listed below:

MongoDBCassandra
It is cross platform document based databaseIt is distributed high performance database
It is written in C++It is written in Java
It stores data in BSON format in documentIt stores data in sql tabular format
MongoDB has its drivers from Apache and license from AGPLCassandra has its license from Apache
It is designed to deal with JSON like documents and access application fasterDesigned to handle large data across many servers

Q #26) How data integrity can be maintained in MongoDB?

Answer: There are a couple of ways data integrity can be achieved in MongoDB

  • The value of the single field can be set using $set.
  • By setting the value of the field to a specific value only when it is a new document, i.e. $setOnInsert.
  • The item can be pushed into a property of type array using $push.
  • By pulling an item that matches the condition from an array.
  • Changing the value of the numeric field by specified amount with $inc.

Q #27) Describe MongoDB replica Set Master vs Slave.

Answer: Master-slave replication is used for MongoDB methods such as backup, failover, read scaling. Setup of this replication mode has 2 steps, start a master node and one or more slave nodes with the master address.

Use Mongo shell at command line type in order to start master run mongod –master.

Type at the command line in order to start slave run mongod –slave –source master_address, here master_address is the location of the master node.

Q #28) Explain why 32 bit MongoDB version is not suitable for production.

Answer: Total storage size for MongoDB server including data and indexes for 32-bit build MongoDB is 2 GB, which is insufficient for large unstructured data like logs, social media posts, email, image, video, etc. Hence MongoDB is not deployed on 32-bit machines for production. Instead, the 64-bit build of MongoDB does not have any limitation on storage size.

Q #29) List CRUD operations by MongoDB.

Answer: CRUD is the abbreviation as explained C- Create, R – Read, U – Update, and D – Delete.

The commands for each of these operations are as below:

C – Create – db.collection_name.insert();

R – Read – db.collection_name.find();

U – Update – db.collection_name.update();

D – Delete – db.collection_name.remove({“field-name” : “value” });

Q #30) How do MongoDB store images, videos, and other large files?

Answer: Images, Videos, and other large-size files are stored in GridFS specification. GridFS divides large files into chunks or parts. These chunks are stored as a separate documents. The default chunk size of GridFS is 255 kb. GridFS organizes data into 2 Collections. In the first collection file chunks and stored, and in another file metadata about the collection is stored.

Q #31) What is the difference between $all and $in operator in MongoDB?

Answer: Both $all and $in operator are used to filter documents in sub array based on condition.

[{“make”:”Hyundai”, “model” : [“SUV”,”Hatchback”,”Sedan”]},
{“make”: “Skoda”, “model” : [“SUV”,”Sedan”] },
{“make”: “Mahindra”,”model” : [“SUV”,”Hatchback”] } ]

Using $all returns only first two documents

db.vehicles.find ({ model : { $all : [“SUV”,”Sedan”] } } ]

Using $in will return all three documents

db.vehicles.find ({ model : { $in : [“SUV”,”Sedan”] } } ]

$all operator is comparable to AND condition returns documents that satisfy all conditions, whereas $in resembles OR condition returns documents that make any condition in query array.

Q #32) Describe Geospatial Indexes in MongoDB.

Answer: MongoDB geospatial queries interpret geometry on a flat surface or a sphere. Geospatial indexes are of two types – 2dsphere indexes that support only spherical queries (queries that interpret geometries on a spherical surface). 2d indexes support flat queries (i.e. queries that interpret flat surface geometries and some spherical queries).

Q #33) List utilities for backup and restore operations in MongoDB.

Answer: MongoDB shell does not have functions for exporting, importing, backup, or restore, but there are methods created in MongoDB to accomplish these features. The utility scripts provided to get data in or out of the database in bulk are as below:

  • mongoimport
  • mongoexport
  • mongodump
  • mongorestore

Q #34) Describe the Aggregation framework in MongoDB.

Answer: Set of analytical tools within MongoDB for analytics on documents in one or more collections is known as the aggregation framework.

The input from MongoDB collection and pass documents from collection through one or multiple stages, each of them perform different operations on its inputs. The inputs and outputs for all the stages are documents, basically a stream of documents.

Q #35) What is a transaction in MongoDB?

Answer: Transaction is the logical processing unit in a database that includes various database operations, such as read or write.

MongoDB uses two APIs for transactions:

  • Core API – start_transaction and commit_transaction are commands used by core API.
  • Call-back API – In addition to transaction operation, callback API includes logic for TrasientTransactionError and UnknownTransactionCommitResult commit errors.

Conclusion

MongoDB is a document-based NoSQL database with features like sharding, partitioning, indexing, hierarchical data storage, and scalability with ease of performance tuning for large unstructured data storage in a format similar to JSON.

Written in C++, MongoDB runs on free RAM space and does not support primary – foreign key relationships. It is highly available, document-oriented, easily scalable and offer high performance for unstructured, complex and large data set such as logs or social media posts, chats, images, and videos.

All the commonly asked questions related to MongoDB have been covered with real-time examples and scenarios in this tutorial.

Was this helpful?

Thanks for your feedback!

Leave a Comment