MongoDB Interview Questions
I am sharing top 41 MongoDB Interview Questions, which will help you to crack the MongoDB Interview.
Q1:- What is MongoDB?
Ans:- MongoDB is a NoSql Database, it is based on BSON.
Q2:- Why is MongoDB not chosen for a 32-bit system?
Ans:- Mongo DB is not considered as a 32-bit system because for running the 32-bit MongoDB, with the server, information and indexes require 2 GB. That is why it is not used in 32-bit devices
Q3:- How to create database?
Ans:- use databaseName
Q4:- How to drop the database?
Ans:- db.dropDatabse() command is used to drop a database.
Q5:- What is a Collection in MongoDB?
Ans- In MongoDB, a collection is a group of MongoDB documents.
Q6:- How to create collection?
Ans:- db.createCollection(name,options)
Q7:- How to drop a collection?
Ans:- db.collectionName.drop()
Q8:- How to insert a document in the collection?
Ans:- db.collectionName.insertOne() is used to add single document in the collection.
db.collectionName.insertMany() is used to add multiple documents in the collection.
Q9:- What is the use of the save() method?
Ans:- The save() method is used to replace the existing document with a new document.
Q10:- Which method is used to update the document?
Ans:- If you want to update single document then use
db.collectionName.updateOne()
If you want to update Multiple documents then use
db.collectionName.updateMany()
Q11:- Which method is used to remove a document from a collection?
Ans:- The remove() method is used to remove a document from a collection.
Q12:- What is MongoDB Projection?
Ans:- Projection is used to select only the necessary data. It does not select the whole data of a document.
readmore…
Q13:- What is the use of the pretty() method?
Ans:- The pretty() method is used to show the results in a formatted way.
Q14:- What is the use of the limit() method?
Ans:- The limit() method is used to limit the records in the database.
Q15:- What is the syntax of the limit() method?
Ans:- The syntax of the limit() method is as follows
>db.COLLECTION_NAME.find().limit(NUMBER)
Q16:- What is the syntax of the sort() method?
Ans:- In MongoDB, the following syntax is used for sorting documents:
>db.COLLECTION_NAME.find().sort({KEY:1})
Note: 1 is used for ascending order.
-1 is used for descending order.
Q17:- What is the syntax of the skip() method?
Ans:- The syntax of the skip() method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Q18:- How to filter the records?
Ans:-
db.users.find({name: /a/}) //like '%a%'
db.users.find({name: /^pa/}) //like 'pa%'
db.users.find({name: /ro$/}) //like '%ro'
Q19:- Which command is used to create a backup of the database?
Ans:- The mongodump command is used to create a backup of the database.
Q20:- Which command is used to restore a backup into the database?
Ans:- The mongorestore command is used to restore into the database.
Q21:- What is the use of an Index in MongoDB?
Ans:- indexes are used to increase the performance to get the records from the database.
Q22:- How to create an index in MongoDB?
Ans:-
db.collectionname.createIndex({'documentkeyname':1})
Q23:- How to get the indexes from the collection?
Ans:-
db.collectionName.getIndexes()
Q24:- Define Namespace in MongoDB?
Ans:- MongoDB stores BSON objects in collections. The concatenation of the database name and the collection name (with a period in between) is called a “namespace”.
Q25:- What is Replication?
Ans:- MongoDB replication is distributed to the data across different machines.
Q26:- What is the use of GridFS in MongoDB?
Ans:- GridFS is used for storing and retrieving large files, such as audio, image, and video files.
Q27:- What is the use of Journaling in MongoDB?
Ans:- Journaling is used for safe backups in MongoDB.
Q28:- What is the use of Profiler?
Ans:- Profiler is used to show the performance characteristics of every operation against the database.
Q29:- What type of data is stored by MongoDB?
Ans:- MongoDB stores data in the form of documents, which are JSON-like field and value pairs.
Q30:- What is the purpose of Replication?
Ans:- Replication provides redundancy, and it increases data availability.
Q31:- What are Embedded documents?
Ans:- Embedded documents capture relationships between data by storing related data in a single document structure.
Q32:- What is Vertical Scaling?
Ans:- Vertical scaling adds more CPU and storage resources to increase capacity.
Q33:- Define Horizontal Scaling.
Ans:- Horizontal scaling divides the dataset and distributes data over multiple servers, or shards.
Q34:- What is Aggregation pipeline?
Ans:- The aggregation pipeline is a framework for performing aggregation tasks. The pipeline is used to transform documents into aggregated results.
Q35:- What is sharding?
Ans:- Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.
Q36:- Which are the storage engines used by MongoDB?
Ans:- MMAPv1 and WiredTiger are two storage engines used by MongoDB.
Q37:- What is the profiler in mongodb?
Ans:- MongoDB Profiler allows you to analyze all the queries which are being run by the database system.
Note:- MongoDB would be to use the query profiler to track and diagnose slow queries.
Q38:- MongoDB store data in which format?
Ans:- MongoDB uses documents to store data into the collections.
Q39:- What is replica set in MongoDB?
Ans:- A replica can be specified as a group of mongo instances that host the same data set. In a replica set, one node is primary, and another is secondary. All data is replicated from primary to secondary nodes.
Q40:- What is primary and secondary replica set in MongoDB?
Ans:- In MongoDB, primary nodes are the node that can accept write. These are also known as master nodes. The replication in MongoDB is a single master so, only one node can accept write operations at a time.
Secondary nodes are known as slave nodes. These are read-only nodes that replicate from the primary.
Q41:- What is Ad-hoc Queries?
Ans:- Ad-hoc queries are the queries not known while structuring the database. So, MongoDB provides ad-hoc query support which makes it so special in this case.