Mongodb Interview Questions And Answers

mongodb interview questions and answers   Top 35 Mongodb Interview Questions And Answers

1) What is MongoDB?

It is cross platform database which is document oriented, provides high performance, availability and easy scalability.


2) What is collection in MongoDB?

It is group of documents. It is same as RDBMS tables. A collection exist within single database. Documents within collection can have different fields.


3) What is document in MongoDB?

It is a set of key-value pairs. Documents have dynamic schema.


4) In RDBMS, we have Table which is similar to what in MongoDB?

Collection


5) In RDBMS, we have Tuple/Row which is similar to what in MongoDB?

Document


6) In RDBMS, we have Column which is similar to what in MongoDB?

Field


7) In RDBMS, we have Table join which is similar to what in MongoDB?

Embedded documents


8) What is database server in MongoDB?

mongod


9) In MongoDB, do we have relationship between tables just like RDBMS?

No, there is no concept of relationship in MongoDB.


10) What are the key advantages of MongoDB over RDBMS?

Key advantages of MongoDB are:

● MongoDB is a document database where one collection holds different documents.
● Don't have complex joins which makes it easy to use.
● It has deep query ability.
● It is easy to scale.
● Enables fast access to data.


11) In MongoDB, why it enables fast access to data?

MongoDB uses internal memory to store the working set so it enables faster access to data.


12) Why MongoDB?

● Document Oriented Storage
● High availability
● Auto-sharding
● Deep and Rich queries
● Fast in-place updates


13) Where we need to use MongoDB?

● Big data
● Content Management and Delivery
● User Data Management
● Mobile and Social Infrastructure


14) How we can create database in MongoDB?

Use DATABASE_NAME is used to create database.

use DATABASE_NAME


15) What will happen if we try to create database using use command and database with same name already exist in MongoDB?

If database already exist then it will return that database otherwise it will create new database.


16) How you can check your currently selected database in MongoDB?

This can be done using db command.

> db


17) How you can check the list of databases in MongoDB?

This can be done using show dbs command.

> show dbs


18) What is the default database in MongoDB?

In MongoDB default database is test


19) In MongoDB, suppose you have create the database but it is not showing in database list, what can be the reason for this?

The reason is that database don't have any document. In order to display the database in the list it must have at least one document.


20) In MongoDB how to drop a database?

db.dropDatabase() command is used to drop a existing database in MongoDB.

db.dropDatabase()


21) In MongoDB how to create collection?

db.createCollection(name, options) is used to create collection in MongoDB.

db.createCollection(name, options)


22) How can we drop a collection in MongoDB?

We can drop the collection from database using db.collection.drop() .

Basic syntax:

db.COLLECTION_NAME.drop()


23) MongoDB supports which all datatypes?

Following are the datatypes which MongoDB supports:

● Integer
● Boolean
● String
● Double
● Arrays
● Timestamp
● Date
● Code
● Regular expression


24) How you can insert document in MongoDB collection?

To insert documentation you need to use insert() method. Also you can use db.post.save(document).

Basic syntax:

>db.COLLECTION_NAME.insert(document)


25) How you can query document from MongoDB collection?

To query data from collection you need to use find() method.

Basic syntax:

>db.COLLECTION_NAME.find()


26) What is the difference between update() and save() methods in MongoDB?

In MongoDB update() method update the existing document whereas save() method replaces the existing document with the document passed in the method.


27) What is remove method in MongoDB?

The remove() method is used to remove the document from the collection.

Basic syntax:

>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)


28) In MongoDB, how you can remove only single record from database?

Single record can be deleted using justOne parameter in remove() method. Just set this parameter as one.

Basic syntax:

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)


29) What is projection in MongoDB?

It means select only the necessary data rather than selecting the complete data.


30) How you can restrict the records in MongoDB?

This can be achieved using limit() method.

Basic syntax:

>db.COLLECTION_NAME.find().limit(NUMBER)


31) What does skip() method do in MongoDB?

The skip() method is used to skip the number of documents.

Basic syntax:

>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)


32) How we can sort the records in descending order in MongoDB?

For this we can use sort() method with -1 as sorting order parameter for descending order.

Basic syntax:

>db.COLLECTION_NAME.find().sort({KEY:-1})


33) How you can create indexes in MongoDB?

The ensureIndex() method can be used to create indexes.

Basic syntax:

>db.COLLECTION_NAME.ensureIndex({KEY:1})


34) How you can insert the value of an array in document in MongoDB?

This can be done using aggregate() method.

Basic syntax:

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])


35) Does MongoDB support pipeline concept?

Yes, similar to UNIX MongoDB also supports pipeline concept.