MongoDB Interview Questions and Answers- Part 3
Moving from relational databases to NoSQL? You’re not alone. MongoDB has become the go-to NoSQL database for developers working on dynamic, high-performance apps. But if you’ve only worked with SQL in the past, MongoDB might seem very different. That’s why we’ve created this MongoDB interview guide—to help you understand how things work in a document-based database and get comfortable with its terminology and logic.
Here, you’ll find questions and answers that cover key areas like schema design, data replication, sharding, aggregation pipelines, and performance tuning. The goal is to help you confidently explain your understanding of MongoDB and how it compares to SQL-based systems.
Answer:
CreateIndex() method constructs the index on a specified column(s) of a specified table. Indexes are fundamentally used to improve database performance. The key field for an index is specified as the column names or as an expression written in parentheses. Multiple fields can be specified if an index method supports multiple column indexes.
Answer:
The oplog or operations log refers to a special capped collection with the rolling record of all operations that modify data stored in databases. MongoDB applies the database operations on the primary node, then records those operations on the primary’s oplog. The secondary nodes then copy and apply those operations in the asynchronous process. All replica set members contain an oplog’s copy in the local.oplog.rs collection. It enables them to maintain the database’s current state.
Answer:
Vertical scaling is an attempt to boosts the capacity of a single machine, hardware, or software by adding more resources. Here resources like memory, processing power, storage, and others are added to the existing work unit. It can improve your server without manipulating the code. However, vertical scaling is limited as it can only add resources as per the server’s size.
Answer:
Horizontal scaling or scale-out requires a load-balancer program, a middle-ware component in the standard three-tier client-server architectural model. The load balancer’s responsibility is to distribute user requests (load) amongst various backend systems, machines, or nodes in the cluster. Each backend machine runs a copy of the software; thus, it is capable of servicing requests. It can also run a “health-check” where a load balancer exchanges heartbeat messages with all servers or uses a ping-echo protocol to ensure they are actively running.
Answer:
A MongoDB sharded cluster has these components:
- Shard: Each separate shard contains a subset of sharded data. In MongoDB, 3.6, shards must be deployed as the replica set.
- Mongos Instances: The mongos act as a query router that provides an interface between a sharded cluster and the client applications. In MongoDB, 4.4 mongos supports hedged reads to reduce the latencies.
- Config Servers: It stores configuration settings and metadata for a cluster. For MongoDB, 3.4 config servers must be deployed as a (CSRS) replica set.
Answer:
The pretty() method is used mainly to display the result in an easy-to-read format.
Answer:
The remove() method of MongoDB helps to remove a document from the collection. It accepts two parameters: deletion criteria and justOne flag.
Answer:
In MongoDB, projection is a way to fetch only the necessary fields of a document from the database. It lessens the amount of data that has to be transferred from a database server to a client; thus, it increases performance.
Answer:
You should use the limit() method to limit the records in MongoDB. This method accepts a number type argument or the number of documents you want to display.
Answer:
Grouping of documents in MongoDB is known as a collection. It is equivalent to an RDBMS table. A collection exists in a single database, and it does not enforce a schema.
Answer:
DB or Database command is used to designate the external database as the current database. In simple words, it designates a database to which the next SQL queries in the current process will be sent.
Answer:
The update() and save() methods in MongoDB are used to update a document in the collection. The update() method updates values in an existing document, whereas the save() method replaces an existing document with a document passed in it.
Answer:
The restore command in the Linux system helps to restore files from the backup created using dump. This command performs the inverse function of the dump. A complete backup of the file system is being restored, and subsequent incremental backups layered is kept on top of it.
Answer:
In MongoDB, dot notation accesses the match by specific fields in an embedded document. When a field holds the array of an embedded document, then to match a field, concatenate a field’s name that contains an array with a dot (.) and field’s name in an embedded document.
Answer:
Auditing refers to the financial statement audit. Its main objective is to evaluate and examine an organization’s financial statements to ensure that its financial records are AN accurate representation of its transaction. An audit can be internally conducted by a company’s employees or externally conducted by an outside CPA firm.
Answer:
Aggregation pipeline in MongoDB is modeled on data processing pipelines. Here, several documents enter a pipeline; then those documents are further transformed into aggregated results. The operations performed during the aggregation pipeline include filter & document transformation wherein they operate such as queries and modify an output document’s form.
Answer:
MapReduce means a program model and processing technique for distributed computing based on Java. A MapReduce algorithm includes two important tasks, i.e., Map and Reduce. The map takes a data set and converts it into another data set, where individual elements are broken into tuples or key-value pairs. On the other hand, reduce task takes the output from the map as the input and combines the data tuples into smaller tuples set. As per the sequence of MapReduce’s name, reduce is always performed after the map.
Answer:
The save() method is mainly used to replace an existing document with a new document.
Answer:
MongoDB is mostly used as the primary data store for operational apps with real-time needs. It is the best fit for 60 to 80 percent of modern applications. MongoDB is easy to scale and operate in the ways that are challenging with relational databases. It excels in different cases where relational databases are inefficient, such as applications with semi-structured, unstructured, and polymorphic data or apps with large scalability needs or multi-datacenter deployments.
MongoDB may not work well for applications that require complex transactions and scan-oriented apps that access large data subsets. Also, MongoDB is not a good replacement for legacy applications built around SQL and relational data model. MongoDB is commonly used for mobile applications, real-time personalization, content management, product catalogs, and applications delivering one view across multiple systems.
Answer:
Yes, null values are allowed in MongoDB, but only for an object’s members. It cannot be added to a database collection as it is not an object.