1. What Are Other Types of NoSQL Databases?
As an interviewer, you would expect a good candidate to know why they’d want to use a NoSQL database, what the purpose of using it is, and so forth. They should also be aware of other NoSQL databases apart from MongoDB, and when to use other NoSQL databases.
Encourage your candidate to provide examples or scenarios of when they might use other types of NoSQL databases. Here are some other NoSQL databases:
- Document databases – Stores data in documents (XML, JSON, YAML, BSON, etc.). Examples of document databases include: Amazon SimpleDB, Apache CouchDB, MongoDB, and Couchbase Server.
- Key-Value (KV) stores – The simplest database type, a key-value database stores data in pairs (attributes, or “keys,” and “values”). Examples of key-value databases include: Apache Ignite, Riak, and Amazon’s DynamoDB.
- Column-oriented databases – These are NoSQL databases which store data in columns, and they are widely used to manage data warehouses, business intelligence, CRMs, library card catalogs, etc. Examples of columnar databases include: Apache Cassandra and Apache HBase.
- Graph databases – Graph databases are mostly used to find connections between data elements. Examples of graph databases include: ArangoDB, Amazon Neptune, Neo4j, and Stardog.
2. What Is a Document in MongoDB?
A document is like a unit where you can store your MongoDB data (JSON-styled). This document will again contain field names and values.
For example, in the below simple example of a JSON document, the site is a “field” and arc.dev is a “value”.
{ site: “arc.dev” } //field: value
You want your candidate to know about MongoDB’s _id
field and how to use them. This is because all documents in MongoDB must have a populated _id
field. If not, MongoDB will generate one for you.
var mydoc = {
_id: ObjectId(“353329803df3f4948bd2345fd391”),
name: { first: “John”, last: “Doe” },
birth: new Date(‘Apr 12, 1912’),
death: new Date(‘Jun 07, 1954’),
contribs: [ “Turing machine”, “Turing test”],
views : NumberLong(1250000)
}
3. How Do You Create a Collection in MongoDB?
The basic step in any MongoDB application is used to create a database and a collection. Because the database stores the information about the collection and the collection in turn stores all the documents.
So when you ask about a collection, your candidate must know how to create a database using the use
command and create a collection using the createCollection()
method. They must also know the different ways to create a collection.
To best answer this question, they’d ideal show how to create a collection using the two methods below:
Method 1: On the fly
In a single line of command, you can insert a document into the collection. MongoDB creates a collection for you on the fly.
Syntax: db.collection_name.insert({key:value, key:value…})
Method 2: Create options before inserting the documents
When you create a collection before you insert the data, you can have the flexibility to set it while creating a collection.
Syntax: db.createCollection(name, options)
4. What Are Indexes in MongoDB?
When you query a MongoDB database by using specific conditions to retrieve documents, the database performs a scan in your collection to retrieve the result. i.e., it will retrieve every document from the collection to verify whether they match the condition. If there is a match, it will be added to the list of returned documents. If not, MongoDB scans the next document until it has scanned the entire collection.
When you ask this question, you are looking for how well the interviewee knows about the impact of the indexes, how indexes work, and how they are implemented in MongoDB.
The best answer about indexes’ impact should include an example of the products in an eCommerce store, where each product is represented by a document containing images, descriptions, prices, and more. Without indexes, MongoDB retrieves all products from the collection. With an index, MongoDB can use a smaller list containing pointers to only products in stock.
{
Empid: 1
Empcode: ABC
Empname: “John”
Country: “India”
}
5. What Are Geospatial Indexes in MongoDB?
As a technical interviewer, you may ask this question to see if your candidate’s familiar with the different types of queries they can use to execute on a collection containing geospatial shapes and points. Their answer should include both types of geospatial indexes in MongoDB.
There are two geospatial indexes in MongoDB: 2dsphere
and 2d
. If you are working with spherical geometries based on the WGS84 datum, then you can use 2dsphere
. In this WGS84 datum model, there is some flattening at the poles near the surface of the earth. You can also specify geometries for lines, points, and polygons in the GeoJSON format.
{
“name”: “New York City”,
“loc” : {
“type” : “Point”,
“coordinates” : [50, 2]
}
}
6. What Is Replication and Why Do You Use It?
Replication means synchronizing data across multiple servers, which increases data availability. In case you lose a single server, then your data is still intact and protected by being stored on other servers.
Here, you want to gauge the candidate’s understanding of the concept of replication. A good answer should cover how replication may help in their application or how they’d protect the database upon loss of a single server or a hardware failure. On top of that, you may want to ask about past application infrastructure they’ve worked with and whether they’ve used multiple servers with data replication before.
How does replication work?
MongoDB uses replica sets to achieve replication. The replica sets are a collection of different MongoDB instances that can act as hosts to the datasets. There are ideally two types of replica sets:
Primary replica set – This is the master replica set to which MongoDB writes data (receives all write operations).
Secondary replica set – This is a replica of the primary replica set’s oplog (operations log, records all operations that modify data in the replica set).
7. What Are Embedded Documents in MongoDB?
Embedded documents in MongoDB allow you to store all kinds of information related to each other in a single document. An embedded (nested) MongoDB Document is a normal document that resides inside another document within a MongoDB collection.
The following are a few general guidelines of common scenarios which call for using embedded documents:
- When your application is using a one-to-few relationship
- When updates to your document are likely to happen at the same time
- When the field is rarely updated
Some key points to look for in an answer about embedded documents include:
- In MongoDB, the size of the document must be smaller than the maximum BSON document size (16MB).
- If you delete the parent MongoDB document, then child MongoDB documents are also deleted.
8. What Is Sharding in MongoDB?
Sharding is the process MongoDB uses to distribute data across multiple hosts.
When you are developing complex applications with large amounts of data within MongoDB, it can impact CPU utilization. The solution is to split these large data sets into small data sets across different MongoDB instances, which can be achieved by a concept in MongoDB called sharding.
9. What Is Map-Reduce in MongoDB?
You may ask this question to gauge your candidate’s understanding of different data processing techniques when they use large data in their applications.
The map function is called a mapper (since it creates a map of data in the form of key-value pair) and why the reduce function is called a reducer (since it aggregates the data that the mapper has generated).
Map-reduce commands in MongoDB are made up of two higher-order functions: map()
and reduce()
. From the name, it implies that the map()
job is performed before the reduce()
job.
10. How Can We Sort the User-Defined Function?
For example, if x and y are integers, how do we calculate “x-y”?
So, to calculate x-y, we use the below function:
db.eval(function() {
return db.scratch.find().toArray().sort(function(arg1, arg22) {
return arg1.a – arg2.a
})
});
//The equivalent client-side sort:
db.scratch.find().toArray().sort(function(arg1, arg2) {
return arg1.a – arg2.b
});