MongoDB Interview Questions And Answers

Define mongodb?

MongoDB is a NoSQL open source document-oriented database management system. This is suitable for modern internet applications. It provides dynamic queries, scalability, secondary indexes, fast atomic updates and we can easily use more than one database in a project.
Who developed MongoDB and when?

It is written in C++, developed by 10gen and licensed under the GNU-AGPL. MongoDB v1.0 was the first version and released in November 2009.
Give some features of MongoDB.

There are the some features of MongoDB -
  • MongoDB is a document-oriented database, it makes the scaling very easier.
  • It is extremely fast and provides key-value storage.
  • Relational databases are more difficult to scale, the MongoDB scales easily and capable to store rich data structures.
  • In a relational database, we need to make the structure first before capturing data. But in mongodb we can easily capture data whose structure can't be known in advance.
  • MongoDB uses the BSON binary format.
What is the limitations to run mongodb on 32 bits OS?

It is recommended to run MongoDB on 64-bits machines. To run on 32-bits systems, you should have at least 4GB of memory.
What is the default port of mongodb server?

The mongodb server by default start on port 27017.
What is the role of use command?

MongoDB use command is used to create a new database or to choose the existing database for operation. If it does not exist, then it will create a database of the specified name.
How do you display the list of all collections of current database?

The mongodb show collections command returns all the collections of the current database.
What is BSON?

BSON (binary JSON), is a binary serialization format and used to store documents and make remote procedure calls in MongoDB.
Give any 6 data types of MongoDB?

These are the six data types of mongodb -
  • double
  • string
  • object
  • undefined
  • object id
  • boolean
What is the difference between min key and max key?

The min key compares the value of the lowest BSON element and the max key compares the value against the highest BSON element.
What is the syntax of mongodb collection?

Syntax of createCollection()
db.createCollection(name, options)

Here, name is the name of the collection and of string type and options is an optional parameter.
What is the need to provide size option on create collection?

The size option specifies a maximum size limit in bytes for a capped collection.
What are the different methods to insert document?

These are the different methods to insert document -
  • insert()
  • insertOne()
  • insertMany()
What is the difference between insert() and insertOne()?

The insert() method inserts one or more documents into a collection while the inertOne() method is used to insert only one document into a collection.
What is the syntax of update document?

Syntax of Update Document
db.collection.update(filter, update, options)

Here, the filter is the selection criteria for updating the data, update is the data on which the update applies and the third parameter is the options which is an optional field. The options can be upsert, multi, collation, arrayFilters etc.
Which method is used to update multiple document?

The updateMany() method is used to update multiple documents.
db.collection.updateMany(filter, update, options)
How do you delete a document from a collection?

The remove() method of mongoDB is used to delete the existing document or documents from a collection.
db.collection.remove(query, justOne)
Here, both are optional parameters, query is the query document and justOne is boolean, if it is set to true or 1, then it removes only one document.
Define the mongodb aggregation?

Aggregation process returns an average of all numeric values seen during the group and project. It ignores non numeric values.

Syntax of Aggregation($avg) - used in $group
{ $avg: expression }
It returns the collective average of all numeric values that result from applying a specified expression to each document in a group of documents. It is available in the group and project stages.

Syntax of Aggregation($avg) - used in $project
{ $avg: [expression1, expression2,..]}

It returns the average of the specified expression or list of expressions for each document. It is available in the group and project stages.
Define mongodb SUM?

MongoDB provides $sum to get sum of numeric values and ignores non numeric values.
Syntax of $sum
{$sum : expressions}

Here, the sum returns the sum of the specified expression or list of expressions. It is available in the group and project stages.
What is the term match in mongodb?

The mongodb match filters the documents so that you can run an aggregation on a subset of documents that match a specific condition.
What is the difference between $lt and $lte?

The $lt symbol specifies 'less than' and $lte specifies 'less than equals to'.
What is syntax of for loop in r?

Syntax of the for loop in R
for(value in vector){
What is the symbol of logical OR operation in mongodb?

Mongodb provides $or operator to perform logical OR operation. It checks whether any of the specified condition match in the document or not.
What is the exist operator?

Mongodb exists operator checks the existence of field in the specified collection.
{ field: { $exists: boolean }}

If $exists is true, it matches the document that contains the field and if $exists is false, it returns only the documents that do not contain the field.
How do you limit the number of result returned?

MongoDB limit() function is used to limit the number of results returned.
Syntax of Mongodb limit
What is the use of mongodb sort()?

Mongodb sort() function is used to sort the result. The sort() function takes an object as parameter. The parameter is in a set of key/value pairs where the keys are key names and the values are the sort direction. This direction can be either ascending(1) or descending(-1).
What is skip() method?

Mongodb skip() method skip the specified number of documents in the query result.
What is the meaning of projections in mongoDB?

In MongoDB, projection means selecting only the necessary data rather than selecting all document fields.
Syntax of Projections
db.collection.find({}, {KEY:1 or 0});

Here, the KEY is the field and we can set its value either 1 or 0. If we want to hide the field from result data, then set it to 0 and if we want to show the data, set it to 1. By this, we can control on the find() method and fetch only the required data.
How do you know the all indexes available for a collection?

MongoDB getIndexes() function shows all the indexes available for a collection. Suppose, we already have a students collection. The following query returns all the available indexes -
Which function is used to create an index?

MongoDB createIndex() function is used to create an index.