MongoDB interview questions & answers

    0 Votes

Frequently asked MongoDB interview questions and answers for freshers, 2-4 year experienced developers & DBA's to help you succeed in the job interview.

1) What is MongoDB?

MongoDB is a document database which provides good performance and it is a leading NoSQL. It stores the document data in BSON structure.

2) In which language MongoDB is written?
MongoDB is written in C++.
3) What are the advantages of MongoDB?
Below are the advantages of MongoDB –
  • Cross Platform
  • Provides High Performance
  • Easy scalability
  • No Complex Joins
  • Schema less
  • Available on AWS, Azure etc.

 

4) What is a Database?

A Database is a container of all the Collections. Every database will have a physical file associated in the file system.

5) What is a Collection?

The Collection is group of Documents in MongoDB. In RDBMS, this can be considered as a table which will have list records (documents in MongoDB).

6) What is a Document?

The Document is a set of key-value pairs. In RDBMS, each record in a table is equivalent to document in MongoDB. Documents will have a dynamic schema, meaning, documents in a collection no need to have a same structure/fields.

7) Compare MongoDB and RDBMS?

Below are MongoDB equivalent in RDBMS –

MongoDBRDBMS
Database Database
Collection Table
Document Record/Row
Field Column
Embedded Documents Table Join

8) What is “mongod”?

It is a host process for the database. When we start mongod, it means starting the MongoDB process and run it in the background.

9) What are the parameters for “mongod”?

Mongod Parameters are for following things -

  • Storing the data in the given path. By default – “/data/db”.
  • Port Number, By default – “27017”.

10) What is “mongo”?

It is the command shell used to connect to a specific instance of mongod. When we run the mongo command with no parameters, it will try to connect to, default port number – 27017 and localhost.

11) What is the MongoDB command to switch the databases?

To switch the database in MongoDB use – “use databasename” command.

12) What is NoSQL?

NoSQL provides data retrieval and data storage. No SQL used in Big Data, Web Apps etc. because of simple design and good performance.

13) What are the types of NoSQL?

Below are the types of NoSQL –

  • Key-Value Store – It will have a has table with keys and values. (Eg : Amazon S3)
  • Graph – It uses edge and nodes to represent the data. (Eg: Neo4J)
  • Document Store – It stores data in the form of documents. (Eg: MongoDB)
  • Column Based Store – Each Storage clock contains data from only one column. (Eg: Cassandra)

14) Why to use MongoDB?

Below are the reasons to go for MongoDB –

  • Data are stored in JSON which mean it’s supported in cross platform.
  • Rich Queries
  • Fast updates
  • Support in MongoDB
  • Replication and High Availability

15) Where we can use MongoDB?

Below are the places where we can use MongoDB –

  • Big Data
  • Content Management Systems
  • Mobile Apps
  • Data Management

16) What is the meaning of Namespace in MongoDB?

A Namespace is a combination of Database Name and Collection Name.


17) List out the Languages supported in MongoDB?

Below is the list of languages supported in MongoDB –
  • C
  • C++
  • C#
  • Java
  • Node.JS
  • Perl
  • Php etc.

 

18) How to create a new database in MongoDB?
New database can be created using the command – “use database Name”, This command will create a new database if there is no existing database or else it will return the existing database.
19) What is the command used to check the list of databases in MongoDB?

To check the list of databases we need to use the command called – “show dbs”.

20) What is the meaning of Sharding in MongoDB?

Sharding is the process of keeping the data records in multiple machines. It is an approach supported in MongoDB for data growth. It is like a horizontal partition and each partition is called Shard.

21) How to see the connection used by MongoDB?

To see the connection used by MongoDB, use – “db_adminCommand (“connPoolStats”);”.

22) What is Replication?

Replication is the process of synchronizing data across multiple servers. This will increase the data availability as multiple of copies of data will be stored across multiple servers.

23) What is replica set in MongoDB?

Replica set is the set of MongoDB instances carrying the same data set. In replica set, one is the primary node, and another is secondary. Data replication is done from primary to secondary node.

24) How to insert a document in the collection in MongoDB?

Below is the command to be used for inserting a document in MongoDB –

Db.CollectionName.Insert({“key”: “value” })

25) How to drop a database in MongoDB?

For dropping a database in MongoDB, use the following command –

Db.dropDatabase() – This will delete the selected database.

26) How to create a collection in MongoDB?

To create a collection of Customers in MongoDB, run the below command –

db.createCollection(“Customers”)

27) How we can check the Collections created in MongoDB?

Use the below command to check the Collections exists in the database –

Show collections

28) How we can drop a Collection in MongoDB?

Collection can be dropped using below command –

Db.CollectionName.drop()

29) Why to use Profiler in MongoDB?

MongoDB Profiler is used to check the performance characteristics of operations done against the database.

30) Does MongoDB support Primary Key, Foreign Key relationships?

No. By default, MongoDB does not support Primary Key, Foreign Key relationships.

31) What are the different data types supported in MongoDB?

Below is the list of Data types supported in MongoDB –

  • String
  • Integer
  • Double
  • Boolean
  • Object
  • Object ID
  • Arrays
  • Min/Max Keys
  • Datetime
  • Code
  • Regular Expression etc.

32) Why to use “Code” datatype in MongoDB?

“Code” datatype is used to store the Java script code in a document.

33) Why to use “Regular Expression” datatype in MongoDB?

“Regular Expression” datatype is used to store regular expression in a document.


34) Why to use “Object ID” datatype in MongoDB?

“Object ID” datatype is used to store document’s ID.
35) How to insert a Document in a Collection?
To insert a Document in Collection we can use below commands –
  • Insert - db.CollectionName.Insert(document)
  • Save - db.CollectionName.Save(document)

 

36) What is “ObjectID” is composed of?
ObjectID is composed of following things –
  • Time Stamp
  • Client Machine ID
  • Client Process ID
  • 3 byte incremented counter
37) What are Indexes in MongoDB?

Indexes are used to execute the query efficiently. Without Indexes, MongoDB will perform scan i.e, scanning all the documents in the collection.

38) How to give Indexes in MongoDB?

Indexes can be used like below –

db.COLLECTION_NAME.ensureIndex ( {ColumnName:1} )
ensureIndex() is used for creating an Index and “1” for ascending order, “-1” for descending order.

39) What are the alternatives to MongoDB?

Below are the alternatives to MongoDB –

  • Cassandra
  • Redis
  • Riak
  • CouchDB

40) How to query documents in a Collection in MongoDB?

We can find a required Document in a Collection using command – “find()”

Db.CollectionName.find()

41) What is the method used to format the result in MongoDB?

"pretty()" method is used to format the results.
Db.CollectionName.find().pretty()

42) Give me an example of how we can apply “AND” or “OR” condition while querying a document in a Collection?

We can use “AND” or “OR’’ queries like below –

db. CollectionName.find({$and:[{"Name":"AAA"},{"Address": "Test Addr"}]}).pretty()
db. CollectionName.find({$or:[{"Name":"AAA"},{"Address": "Test Addr"}]}).pretty()

43) How to update Documents in MongoDB?

For updating documents, we need to use below method –

db. CollectionName.update({filter criteria}, {$set: {new data}}, {mutli:true})

By default MongoDB update only one Document. To update multiple Documents we need to specify - {mutli:true}.

44) How to delete Documents in MongoDB?

For deleting documents, we need to use below method –

db. CollectionName.remove({filter criteria})

45) Why to use the “Skip()” method in MongoDB?

“Skip()” method is used to skip number of Documents as given in the method.

Skip(3) - Skips 3 Documents.

46) How we can sort the Documents in MongoDB?

To sort the Documents we can use – “Sort()” method.

db. CollectionName.find({filter criteria }).sort({ColumnName:1})

Sort() method accepts the field on which the sorting has to be done and the sorting order –

“1” – Ascending.
"-1" – Descending.

47) What is Aggregation in MongoDB?

Aggregation is used to process data and return the computed results. Aggregation can perform a variety of operations on the grouped data and finally returns the single result. It is like SQL Server Aggregators. 

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

SQL Queries Interview Questions and Answers
Database & SQL Interview Questions
Oracle Interview Questions and Answers on SQL Queries and Database Theory
Advanced SQL Interview Questions and Answers
SQL Server Interview Questions and Answers