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.

Explain the SET Modifier in MongoDB?

Answer»

If the VALUE of a field does not yet exist, the "$set" sets the value. This can be USEFUL for updating schemas or adding user-defined keys.

Example:

> db.users.findOne(){ "_id" : OBJECTID("4b253b067525f35f94b60a31"), "name" : "alice", "age" : 23, "sex" : "female", "location" : "India"}

To add a field to this, we use “$set”:

> db.users.updateOne({"_id" : ObjectId("4b253b067525f35f94b60a31")},... {"$set" : {"FAVORITE book" : "Start with Why"}})
2.

Explain the process of Sharding.

Answer»

Sharding is the process of splitting data up across machines. We also use the term “partitioning” SOMETIMES to describe this concept. We can store more data and handle more LOAD without requiring larger or more powerful machines, by putting a subset of data on each machine.
In the figure below, RS0 and RS1 are shards. MongoDB’s sharding allows you to create a cluster of many machines (shards) and break up a collection across them, putting a subset of data on each shard. This allows your application to GROW beyond the resource limits of a standalone server or replica set.

SHARDED Client ConnectionNon Sharded Client Connection
3.

What are Geospatial Indexes in MongoDB?

Answer»

MongoDB has two types of geospatial indexes: 2dsphere and 2d. 2dsphere indexes WORK with spherical geometries that model the surface of the earth based on the WGS84 datum. This datum model the surface of the earth as an oblate spheroid, meaning that there is some flattening at the poles. Distance calculations using 2sphere indexes, therefore, take the shape of the earth into account and provide a more accurate treatment of distance between, for example, two cities, than do 2d indexes. Use 2d indexes for points stored on a two-dimensional plane.

2dsphere allows you to specify geometries for points, lines, and polygons in the GeoJSON format. A point is given by a two-element array, representing [longitude, latitude]:

{ "name" : "New York City", "loc" : { "type" : "Point", "coordinates" : [50, 2] }}

A line is given by an array of points:

{ "name" : "Hudson River", "loc" : { "type" : "LineString", "coordinates" : [[0,1], [0,2], [1,2]] }}
4.

Explain the term “Indexing” in MongoDB.

Answer»

In MongoDB, indexes help in efficiently resolving queries. What an Index does is that it stores a small part of the data set in a form that is EASY to traverse. The index stores the value of the specific field or set of fields, ordered by the value of the field as specified in the index. 
MongoDB’s indexes work almost identically to typical RELATIONAL database indexes.

Indexes look at an ordered list with references to the content. These in turn allow MongoDB to query orders of magnitude faster. To CREATE an index, use the createIndex collection method.

For example:

> db.users.find({"USERNAME": "user101"}).explain("executionStats")

Here, executionStats MODE helps us understand the effect of using an index to satisfy queries.

5.

How is Querying done 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.

For example: If we have a string we want to MATCH, such as a "username" key with the value "alice", we use that key/value PAIR instead:

> db.users.find({"username" : "alice"})