Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

When to use MongoDB?

Answer»

You should use MongoDB when you are building internet and business applications that need to evolve quickly and scale elegantly. MongoDB is popular with developers of all kinds who are building scalable applications using agile methodologies.
MongoDB is a great choice if one needs to:

  • Support a rapid iterative development.
  • Scale to HIGH levels of read and write traffic - MongoDB supports horizontal SCALING through Sharding, distributing data across several machines, and facilitating high throughput operations with large SETS of data.
  • Scale your data repository to a massive size.
  • Evolve the type of DEPLOYMENT as the business changes.
  • Store, manage and search data with TEXT, geospatial, or time-series dimensions.
2.

What are the data types in MongoDB?

Answer»

MongoDB supports a wide range of data types as values in documents. Documents in MongoDB are similar to objects in JavaScript. ALONG with JSON’s essential key/value–pair nature, MongoDB adds SUPPORT for a number of additional data types. The common data types in MongoDB are:

  • Null
    {"x" : null}
  • Boolean
    {"x" : true}
  • Number
    {"x" : 4}
  • String
    {"x" : "foobar"}
  • Date
    {"x" : new Date()}
  • Regular EXPRESSION
    {"x" : /foobar/i}
  • Array
    {"x" : ["a", "b", "c"]}
  • Embedded document
    {"x" : {"foo" : "bar"}}
  • OBJECT ID
    {"x" : ObjectId()}
  • Binary Data
    Binary data is a string of arbitrary bytes.
  • Code
    {"x" : function() { /* ... */ }}
3.

How to perform queries in MongoDB?

Answer»

The find method is used to PERFORM queries in MongoDB. Querying returns a subset of documents in a collection, from no documents at all to the entire collection. Which documents GET returned is DETERMINED by the first argument to find, which is a DOCUMENT specifying the query criteria.

Example:
> db.users.find({"age" : 24})

4.

How do you Delete a Document?

Answer»

The CRUD API in MONGODB provides deleteOne and deleteMany for this PURPOSE. Both of these methods TAKE a filter document as their first parameter. The filter specifies a set of criteria to match against in removing documents.

For example:
> db.books.deleteOne({"_id" : 3})

5.

How do you Update a Document?

Answer»

Once a document is stored in the database, it can be changed USING one of several UPDATE methods: updateOne, updateMany, and replaceOne. updateOne and updateMany each takes a filter document as their first parameter and a modifier document, which describes changes to make, as the second parameter. replaceOne also takes a filter as the first parameter, but as the second parameter replaceOne expects a document with which it will replace the document matching the filter.

For example, in order to replace a document:

{ "_id" : ObjectId("4b2b9f67a1f631733d917a7a"), "NAME" : "alice", "friends" : 24, "enemies" : 2}
6.

How to add data in MongoDB?

Answer»

The BASIC method for adding DATA to MongoDB is “inserts”. To insert a single document, use the collection’s insertOne method:

> db.books.insertOne({"TITLE" : "START With Why"})

For inserting multiple documents into a collection, we use insertMany. This method enables passing an ARRAY of documents to the database.

7.

What are some features of MongoDB?

Answer»
  • Indexing: It supports generic secondary indexes and PROVIDES unique, compound, geospatial, and full-text indexing capabilities as well.
  • Aggregation: It provides an aggregation framework based on the concept of DATA processing pipelines.
  • Special collection and index types: It supports time-to-live (TTL) COLLECTIONS for data that should expire at a certain time
  • FILE storage: It supports an easy-to-use protocol for storing large files and file metadata.
  • Sharding: Sharding is the process of splitting data up across machines.
8.

How does Scale-Out occur in MongoDB?

Answer»

The document-oriented data model of MongoDB makes it EASIER to split data across multiple servers. Balancing and loading data across a cluster is done by MongoDB. It then redistributes documents automatically.

The mongos acts as a query router, providing an interface between CLIENT applications and the sharded cluster.

CONFIG servers STORE metadata and configuration settings for the cluster. MongoDB uses the config servers to manage DISTRIBUTED locks. Each sharded cluster must have its own config servers. 

9.

What is the Mongo Shell?

Answer»

It is a JavaScript shell that allows interaction with a MongoDB instance from the COMMAND LINE. With that one can perform administrative functions, inspecting an instance, or EXPLORING MongoDB. 

To start the shell, run the mongo EXECUTABLE:

$ mongod$ mongoMongoDB shell version: 4.2.0connecting to: test>

The shell is a full-featured JavaScript interpreter, capable of running arbitrary JavaScript programs. Let’s see how basic math works on this:

> x = 100;200> x / 5;20
10.

What are Databases in MongoDB?

Answer»

MONGODB groups collections into DATABASES. MongoDB can host several databases, each grouping TOGETHER collections. 
Some reserved database NAMES are as follows:
admin
local
config

11.

What is a Collection in MongoDB?

Answer»

A collection in MongoDB is a group of documents. If a document is the MongoDB analog of a row in a relational database, then a collection can be thought of as the analog to a table.
Documents within a single collection can have any number of DIFFERENTSHAPES.”, i.e. collections have dynamic schemas. 
For example, both of the following documents COULD be stored in a single collection:

{"greeting" : "Hello WORLD!", "VIEWS": 3}{"signoff": "Good bye"}
12.

What is a Document in MongoDB?

Answer»

A Document in MongoDB is an ORDERED SET of keys with associated values. It is represented by a map, hash, or dictionary. In JavaScript, DOCUMENTS are represented as objects:
{"greeting" : "Hello WORLD!"}

Complex documents will contain MULTIPLE key/value pairs:
{"greeting" : "Hello world!", "views" : 3}

13.

What are some of the advantages of MongoDB?

Answer»

Some advantages of MongoDB are as follows:

  • MongoDB supports field, range-based, string pattern matching TYPE queries. for searching the data in the database 
  • MongoDB SUPPORT primary and secondary index on any fields
  • MongoDB BASICALLY USES JavaScript objects in PLACE of procedures
  • MongoDB uses a dynamic database schema
  • MongoDB is very easy to scale up or down
  • MongoDB has inbuilt support for data partitioning (Sharding).