Call: IN +91 7013283324 | +91 9347854179
(Click on WhatsApp Chat button)
Call: US +1 (415) 529-7271

Top 50 MongoDB Interview Questions and Answers

Home  >>  Interview Questions  >>  Top 50 MongoDB Interview Questions and Answers

Top 50 MongoDB Interview Questions and Answers

On September 17, 2021, Posted by , In Interview Questions, With Comments Off on Top 50 MongoDB Interview Questions and Answers
MongoDB_interview_Questions
MongoDB_interview_Questions

MongoDB is one of the most popular and widely used databases in the world. Earlier, the data was structured and SQL was primarily used to deal with it. But with the changing internet and technology, the data is also transforming. Today, unstructured data is being used widely. To deal with such data, NoSQL databases have emerged and MongoDB is the most popular NoSQL database.

MongoDB is highly in demand. It is a core part of MEAN and MERN web software stacks. So in this article, we will list fifty MongoDB questions with answers. 

Question No. 1: What is MongoDB?

Answer No. 1: MongoDB is a cross-platform open-source document-oriented NoSQL database. It is the most widely used NoSQL database in the world.

Question No. 2: How does MongoDB store data?

Answer No. 2 : MongoDB stores data in the form of JSON documents.

Question No. 3: What are the features of MongoDB?

Answer No 3: The features of MongoDB are:

  • It is faster and high-performing.
  • It provides efficient indexing.
  • It offers useful features such as sharding and transactions.
  • It is easier to write queries.

Question No. 4: Name the programming languages that can be used with MongoDB.

Answer No. 4: The programming languages that can be used with MongoDB are Java, C, C++, Node.js, Python, PHP, Scala, Erlang, and Ruby. 

Question No. 5: How is MongoDB better than MySQL?

Answer No 5: MongoDB is better than MySQL because:

  • MongoDB is faster than MySQL.
  • MongoDB is easier to learn and use.
  • MongoDB offers better flexibility.
  • MongoDB is easier to scale.

Question No. 6: When to use MongoDB?

Answer No 6: MongoDB should be used when:

  • There is a huge amount of unstructured data.
  • Cloud computing and storage are being considered.
  • Development time is less.

Question No. 7: Give a scenario explaining MongoDB is better than RDBMS.

Answer No 7: Suppose we have a table in RDBMS with three columns – id, name, and age. Now suppose we have a new record that contains the id, name, age, and location. If we try to add this record to the existing table, it will throw an error. We have to alter the table before adding the record. But no alteration is required with MongoDB because we do not need to add any schema with it. 

Question No. 8: How to create a database in MongoDB? How to find all the databases?

Answer No 8: The “use” command is used to create a database in MongoDB. The following command creates a database named “company”.

use company

To get the list of all the databases, use the “show” command like the following.

show dbs

Question No. 9: What is a document in MongoDB?

Answer No 9: A document is a JSON-like structure that holds a record in MongoDB.

Question No. 10: What is a collection in MongoDB?

Answer No 10: A collection is a group of documents. In other words, a collection is where the documents are stored. 

Question No. 11: Suppose there are two records – A and B. Document A has the name and age of an employee while document B has the location and phone number of another employee. Do you think these two documents can be added to the same collection? Justify your answer.

Answer No 11: Yes, it is possible to add these two documents to a collection but only if no schema is defined.

Question No. 12: Suppose there is a collection in which every document holds the information of an image. It has one field named “tag” and it can have multiple values. How can we store multiple values in a document for a single field?

Answer No 12: In MongoDB documents, we can also use arrays to store values. So in such a case, we can use an array to store the multiple values of the “tag” field.

Question No. 13: Give the different ways of creating collections in MongoDB.

Answer No 13: The different ways of creating collections in MongoDB are:

  • by using the createCollection() method.
  • by using the createIndex() method.
  • by using insertOne() or insertMany() method.

Question No. 14: Which field is created automatically for every document? 

Answer No 14: The “_id” field is created automatically by MongoDB for every document. 

Question No. 15: What is the significance of “_id”?

Answer No 15: The “_id” denotes the unique index for every document in a collection. The value of “_id” will be different for every document in a collection. 

Question No. 16: Whenever a document is returned from a collection, it has the automatically created “_id” field in it. How to avoid the “_id” field?

Answer No 16: The “_id” field can be avoided in the returned document by setting the value of “_id” as 0 in the query. 

Question No. 17: What is the difference between drop() and remove()?

Answer No 17: The drop() method removes the entire collection including indexes while the remove() method removes the documents from the collection.

Question No. 18: Explain the different methods for inserting documents in a collection.

Answer No 18: There are two methods used for inserting documents in a collection.

  • insertOne: It is used to insert one document in a collection.
  • insertMany: It is used to insert one or multiple documents in a collection.

Question No. 19: What happens when insertOne() or insertMany() are used on collections that do not exist in the database?

Answer No 19: In such cases, MongoDB automatically creates a new collection using the specified name.

Question No. 20: Suppose there is a collection named “employees” and it has the following three documents in it:

{
“_id”: “507c35dd8fada716c89d0013”,
“name”: “John”,
“age”: 21,
“YOJ”: 2016
},
{
“_id”: “507c35dd8fada716c89d0014”,
“name”: “Mike”,
“age”: 25,
“YOJ”: 2016
},
{
“_id”: “507c35dd8fada716c89d0015”,
“name”: “Leh”,
“age”: 34,
“YOJ”: 2016
}

What will happen if the following query is executed:

db.employees.findOne({YOJ: 2016})

Answer No 20: It will return only the first document, i.e.

{
“_id”: “507c35dd8fada716c89d0013”,
“name”: “John”,
“age”: 21,
“YOJ”: 2016
}

Question No. 21: Write a MongoDB query to find all the documents in the collection “employees” where “YOJ” is greater than 2015.

Answer No 21: db.employees.find({YOJ: {$gt : 2015}})

Question No. 22: What happens when the findOne() method matches more than one document?

Answer No 22: In such cases, the findOne() method returns the first matched document while others are ignored.

Question No. 23: Suppose we have a collection named “employees” and it contains the following documents.

{
“_id”: “507c35dd8fada716c89d0013”,
“name”: “John”,
“age”: 21,
“YOJ”: 2016
},
{
“_id”: “507c35dd8fada716c89d0014”,
“name”: “Mike”,
“age”: 25,
“YOJ”: 2016
},
{
“_id”: “507c35dd8fada716c89d0015”,
“name”: “Leh”,
“age”: 34,
“YOJ”: 2016
}

Write a query that returns documents with only “name” and “age” fields. 

Answer: db.employees.find({ }, { _id: 0, name: 1, age: 1})

Question No. 24: – What is the difference between updateOne() and replaceOne()?

Answer No 24: The updateOne() method updates the content of the matched document while the replaceOne() method replaces the entire document that matched with the query. 

Question No. 25: What is the usage of the $set operator in the updateOne() and updateMany() methods?

Answer No 25: The $set operator contains the value that is to be updated in the matched document. 

Question No. 26: Explain the parameters of the find() method.

Answer No 26: The find() method has two parameters:

  • query: It is used to match the documents.
  • projection: It is used to specify which field will be present in the returned documents.

Question No. 27: Observe the following documents of the “employees” collections.

{
“_id”: “507c35dd8fada716c89d0013”,
“name”: “John”,
“age”: 21,
“YOJ”: 2016,
“locations: [{
“city1”: “New York”,
“city2”: “Chicago”,
“city3”: “Paris”
}]
},
{
“_id”: “507c35dd8fada716c89d0014”,
“name”: “Mike”,
“age”: 25,
“YOJ”: 2016,
“locations: [{
“city1”: “Berlin”
“city2”: “New York”,
“city3”: “London”
}]
},
{
“_id”: “507c35dd8fada716c89d0015”,
“name”: “Leh”,
“age”: 34,
“YOJ”: 2016,
“locations: [{
“city1”: “Madrid”
“city2”: “Brampton”,
“city3”: “Moscow”
}]
}

Write a query that returns the document where “city3” is Moscow.

Answer No 27: db.employees.find({ “locations.city3”: “Moscow})

Question No. 28: What is the return value of the deleteMany() method?

Answer No 28: The deleteMany() method returns an object containing two properties:

acknowledged: A boolean value representing if the operation was successful or not.

deletedCount: Total number of documents deleted during the operation.

Question No. 29: What is bulkWrite()?

Answer No 29: The bulkWrite() method is used to perform multiple operations on a collection. For example, by using the bulkWrite() method, we can perform insertOne(), updateOne(), and deleteOne() togather.

Question No. 30: What is Mongoose?

Answer No 30: Mongoose is a library that is used with MongoDB and Node.js for object data modeling. 

Question No. 31: What is the need for mongoose with MongoDB?

Answer No 32: MongoDB is a schemaless database. This is an advantage over SQL databases but it is highly recommended to define a schema for better performance. With the help of mongoose, schema can be defined while maintaining flexibility. 

Question No. 32: Explain document schema.

Answer No 33: A document schema is a structure that is defined to shape the content of a document, meaning, the content of every document will be according to this defined structure.

Question No. 33: Suppose, we want every document to have a mandatory “name” field in it. What should be done to this?

Answer No 34: To do this, we have to use the “required” field on the “name” field while defining the schema.

Question No. 34: What happens when the “uniqueItems” field is set to “true”?

Answer No 35: Every item of that array should be unique if the “uniqueItems” field is set to “true”.

Question No. 35: How to define a regular expression for a string in a schema?

Answer No 36: To define a regular expression for a string in a schema, we have to use the “pattern” field.

Question No. 36: Define a schema with the following conditions.

  • “name” is mandatory.
  • The minimum length of the “name” should be 4 and the maximum length should be “12”.
  • “name” should be a string.

Answer No 36:

const schema = new Schema({
name : { 
type: String,
minLength: 4,
maxLength: 12
required: true
}
})

Question No. 37: Define a schema with the following conditions.

  • “location” should be an array with documents.
  • Each document should have “city1” and “city2”. 
  • “city1”, and “city2” should be strings.

Answer No 37:

const schema = new Schema({
location: [{
city1: String,
city2: String
})

Question No. 38: Explain upsert.

Answer No 38: Upsert is an operation similar to the update but in upsert, if no document matches the query, it inserts a new document in the collection.

Question No. 39: Observe the Employee collection.

{
“_id”: “507c35dd8fada716c89d0013”,
“name”: “John”,
“age”: 21,
“YOJ”: 2016
},
{
“_id”: “507c35dd8fada716c89d0014”,
“name”: “Mike”,
“age”: 25,
“YOJ”: 2016
},
{
“_id”: “507c35dd8fada716c89d0015”,
“name”: “Leh”,
“age”: 34,
“YOJ”: 2016
}

What will happen when the following code is executed in mongoose.

Employee.updateOne(
{ name: “Kane” },
{ $set: { age: 22 } },
{ upsert: true } 
);

Answer No 39: When the code is executed, it inserts a new document in the collection with the “name” field equal to “Kane” and the “age” field equal to 22.

Question No. 40: Suppose there is a collection with ten documents. We have to use the find() method on this collection but we need only the first five documents. How can we do this?

Answer No 40: We can use the limit() method along with the find() method and pass 5 as its argument. For example, the following query will return the first five documents of the “employees” collection.

db.employees.find().limit(5)

Question No. 41: How to connect mongoose with MongoDB?

Answer No 41: To connect mongoose with MongoDB, use the connect() method and pass the Mongo URI as the argument. 

Question No. 42: What is the URI string?

Answer No 44: URI stands for Uniform Resource Identifier. It is a string similar to URL and is required to connect to MongoDB. A URI contains a mandatory MongoDB prefix and hostname while username, password, port, database, and other hostnames are optional. 

Question No. 43: Create a URI with “admin” as both username and password, and “employees” as database.

Answer No 43: mongodb://admin:[email protected]/employees

Question No. 44: What is the population in MongoDB?

Answer No 44: Population is a technique using which the specified paths in a document are replaced by the document(s) of another collection.

Question No. 45: What is the purpose of the model() function in mongoose?

Answer No 45: The model() function in mongoose is used to create a collection with the help of a defined schema. It has two parameters:

  • collection name
  • collection schema

Others

Question No. 46: Explain mongo shell.

Answer No. 46: A mongo shell is a JavaScript shell that is used to interact with MongoDB instances through command prompt.

Question No. 47: What is the usage of indexing in MongoDB?

Answer No. 47: A collection can be huge and scanning through it can be costly in the terms of efficiency and performance. By using indexing, we can avoid this entire scanning of the collection and get the information with better efficiency. 

Question No. 48: What is the aggregation pipeline?

Answer No. 48:The aggregation pipeline is an operation in which data is processed in multiple stages where the result of one stage is transferred as input to the next stage and in the end, the combined result is returned. 

Question No. 49: What will happen when the following query is executed?

db.employees.aggregate([
{ $lookup:
{
from: "onsite",
localField: "name",
foreignField: "emp_name",
as: "total_employees"
}
}
])

Answer No. 49: The above query is joining two databases – “employees” and “onsite” by matching the “name” field of “employees” to the “emp_name” field of the “onsite”, The result will be an array name “total_employees”.

Question No. 50: Explain sharding.

Answer No. 50: Sharding is a technique using which large sets of data can be split into smaller sets of data so that they can be shared across multiple MongoDB instances.

Comments are closed.