Q1) Give a brief explanation of MongoDB along with its history?
MongoDB (from humongous) is referred to as a cross-platform which is designed for a document-oriented database. MongoDB is called a NoSQL database It is released as a combination of the GNU Affero General Public License and the Apache License, MongoDB is an open-source platform. In 2007, the MongoDB was first developed by the software company 10gen (now, MongoDB Inc.) which is referred to as a component of a planned platform as a service-based product. Then, moved to the open-source development model in 2009, with 10gen offering its services to commercial support and other services. Since then, MongoDB has been adopted and called backend software by a number of major websites and services, including Craigslist, eBay, Foursquare, SourceForge, Viacom, and the New York Times, among others. Currently, MongoDB is the most popular NoSQL database system in the market.


Q2) What do you understand by the term NoSQL databases? Is MongoDB called a NoSQL database or not? Explain.
In the current times, we all know that the Internet is loaded and blocked with big data, big users, and so on that are becoming more complex to manage and maintain day by day. NoSQL is the solution to all the above-mentioned problems; it is not a traditional database management system, and also not even a relational database management system (RDBMS).

NoSQL refers to ‘Not only SQL’, and it is referred to as a type of database that is capable of handling and sorting all types of messy, unstructured, unorganized, and complicated data. It is just a new way to think about databases. Yes, MongoDB is a NoSQL database.


Q3) Illustrate the differences between MongoDB and MySQL?
By this time, we all know that MongoDB and MySQL are free and open-source databases, but there is a lot of difference between them in terms of performance speed,data representation, relationships, transaction, schema design, querying data, and definition, normalization, and many more. MySQL and MongoDB will be different in their ways lie the comparison that exists between relational and non-relational databases.


Q4) What kind of a database is MongoDB?
MongoDB is referred to as a document-oriented DBMS. We can consider it as MySQL but it includes JSON-like objects that comprise the data model, rather than RDBMS tables. Significantly, MongoDB provides its support to neither join nor transactions. However, it provides the features that include secondary indexes, hence being called an expressive query language, atomic writes on a per-document level, and fully consistent reads. Operationally, MongoDB is compatible to offer at its best with the master–slave replication along with automated failover and also a built-in horizontal scaling using the automated range-based partitioning.


Q5) What are the points that are needed to be taken into consideration while creating a schema in MongoDB?
Below are the points that need to be taken care of while creating a schema in MongoDB.

  • Designing of the  schema based on the user requirements
  • Combining the objects into one document if we want to use them together; otherwise, separate them as usual.
  • Perform the joins while on write, and do not when it is on reading mode.
  • Optimize the schema for the most frequent use cases.
  • Perform the complex aggregation in the schema as required.

Q6) Define the terms  Auditing and aggregation pipeline?
Auditing: Auditing is one of the terms that is providing the administrators with the capability to perform the verification that the implemented security policies are controlling the activity in the system.

Aggregation pipeline: The aggregation pipeline is referred to a framework that is designed for performing the aggregation tasks. The pipeline is used for transforming the documents into aggregated results.


Q7) Define the term MapReduce?
MapReduce is referred to as a generic multi-phase data aggregation modality that is used responsibly for processing quantities of data.


Q8) What’s a Secondary or Slave?
A secondary is referred to as a node/member which is capable of applying the operations from the current primary. This is performed by tailing the replication oplog (local.oplog.rs). The replication from primary to secondary is said to be asynchronous, however, the secondary will try to ensure to stay as close to current as possible (often this is just a few milliseconds on a LAN).


Q9) Define the term GridFS and explain its functionality in MongoDB?
In MongoDB, GridFS is referred to as a special specification that is designed for storing and retrieving files that exceed the BSON-document size limit (16MB). The primary function of this grid is to divide a particular file into smaller segments and is responsible for storing each of those segments as a separate document rather than storing them into a single document.


Q10) Define the storage engine in MongoDB with an example.
A storage engine is referred to as a part of the database and is specifically used for managing the data storage on the disk. Let us consider an instance where there are two storage engines, one engine might offer its support for only read-heavy workloads, and another storage engine might offer higher throughput for writing operations.


Q11) List out the key features that are provided by MongoDB?
Ans: Below listed are the core features of MongoDB:

  • Rich query language
  • High performance
  • Automatic scaling
  • High availability

Q12) Explain the term indexes in MongoDB?
In MongoDB, Indexes are used for performing the execution of the query efficiently. In the indexes are not used, MongoDB must perform a collection scan, in which scanning of every document in a collection takes place, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can make use of the index in order to limit the number of documents it must inspect.


Q13) Illustrate the differences between MongoDB and CouchDB?
Below are the difference between MongoDB and CouchDB:

  • MongoDB is faster when compared to CouchDB while CouchDB is safer when compared to MongoDB.
  • MongoDB does not provide its support for triggers and is not available in MongoDB while triggers are available in CouchDB.
  • MongoDB also allows serialization of the JSON data to BSON while CouchDB is not capable of storing the data in JSON format

Q14) Illustrate the differences between MongoDB and Cassandra?
Below are the differences between MongoDB and Cassandra:

  • MongoDB is referred to as a cross-platform document-oriented database system while Cassandra is called a high-performance distributed database system.
  • MongoDB is written in the C++ programming language while Cassandra is written in the Java programming language.
  • MongoDB is also providing its support to easily administer in the case of any failure while Cassandra is capable of providing high availability without any single point of failure.

Q15) Do you think that there is a need to create a database command in MongoDB?
There is no need to create a database manually in MongoDB as it creates automatically whenever you save the value into a defined collection the first time.


Q16) Why is a profiler used in MongoDB?
A database profiler is designed for collecting the data about MongoDB write operations, database commands, cursors, on a running MongoDB instance. You are allowed to enable the profiling feature on a per-database or per-instance basis. The database profiler is capable of writing all the data it collects to the system. The profile collection is called a capped collection.


Q17) What is meant by sharding in MongoDB?
In MongoDB, Sharding is referred to as a procedure of storing the data records across multiple machines. Sharding is a MongoDB approach that is allowing us to meet the demands of data growth. It will be creating a horizontal partition of data in a database or search engine. Each partition is referred to as shard or database shard.


Q18) What is meant by a replica set in MongoDB? What is the primary and secondary replica set in MongoDB?
A replica is referred to be specified as a group of mongo instances that will be capable of hosting the same data set. In the replica set, one node will be a primary one, and another will be the secondary.  All data will be replicated from primary nodes to secondary nodes.

In MongoDB, the primary nodes are those nodes that can accept writes. These are also called master nodes. The replication in MongoDB is referred to as a single master, Hence, only one node can accept write operations at a time. The Secondary nodes are known as slave nodes. The secondary nodes are referred to as the read-only nodes that will be replicated from the primary.


Q19) Do you think it is possible to remove old files in the moveChunk directory?
Yes, it is possible to delete the old files once the operations are performed as these files are made as backups during normal shard balancing operations. This is considered as a manual cleanup process and necessary to free up space.


Q20)  Is MongoDB better than other SQL databases? If yes, please explain how?
It is true that MongoDB is better than other SQL databases as it allows a highly flexible and scalable document structure.

For example:

  • One data document in MongoDB can have five columns and the other one in the same collection can consist of ten columns.
  • MongoDB databases are faster than SQL databases due to the concepts of efficient indexing and storage techniques.

Q21) How to perform the transactions/locking in MongoDB?
MongoDB does not make use of the conventional locking with reduction as it is designed to be light, high-speed in its presentation. It can be considered as parallel to the MySQL MyISAM auto entrust sculpt. With the simplest business sustain, performance is high and enhanced, particularly in a structure that includes numerous servers.


Q22) How does MongoDB provide consistency? Why is MongoDB not chosen or applicable for a 32-bit system?
MongoDB makes use of the reader–writer locks, which provides its support for simultaneous readers allowing them to access or view any supply like a database or a collection but always offers private access to single writes. MongoDB is not considered as a 32-bit system because, in order to run the 32-bit MongoDB, with the server, information and indexes require 2 GB.  This is the reason why it is not used in 32-bit devices.


Q23) What is a ‘namespace’?
Ans: MongoDB is capable of storing JSON objects in collections. The namespace is referred to as the concatenation of the database name and the collection name.


Q24) Does an update fsync to disk immediately?
No, an update does not fsync immediately while the writes to disk are lazy by default. A write may only hit the disk a couple of seconds later. Let us consider an example, if the database receives a thousand increments to an object within just a span of one second, then it will only be flushed to disk once. (Note: fsync options are available both at the command line and via getLastError_old.)


Q25) How long does a replica set failover take?

It may take at least a time span of  10-30 seconds for the primary to be declared down by the other members and a new primary to be elected. During this particular time frame, the cluster will be down for primary operations which usually includes the writes and strongly consistent reads. However, eventually, consistent query execution will take place along with the secondaries at any time (in slaveOk mode), including during this window.


Q26) Which programming languages can be used with MongoDB?

MongoDB is capable of being accepted in all programming languages. The following is a list of a few languages:

  • Python
  • Ruby
  • Scala
  • Go
  • Erlang
  • C
  • C++
  • Java
  • C#
  • Node.js
  • Perl
  • PHP

Q27) List out the different index types in MongoDB?

Below is the different types of index types available in Mongo DB:

  • Single field –This index type is used  for indexing and sorting on a single field
  • Multi-key – This index type is used for indexing array data
  • Default – This index type is the _id that MongoDB creates
  • Hashed – This index type is used  for indexing  the hashes of a field value
  • Compound – This index type is used for multiple fields
  • Geospatial – This index type is used to query geospatial(location) data

Q28) Explain the working mechanism of the Journaling work in MongoDB?

Journaling is specifically used for recovering the information after the last checkpoint when MongoDB exits unexpectedly. The storage engine (WiredTiger) is capable of creating a journal record for each of the clients that have initiated the write operation. If there is an update, a single journal record is also capable of recording the update operation as well as index modifications. Journal records are used for storing using the in-memory buffering. Journal files will be stored under the ‘journal’ directory that is created by MongoDB.


Q29) Briefly explain the procedure of starting the MongoDB server/ instance?

In order to start a MongoDB instance, the below steps have to be followed:

  • The first step is to open the command prompt and run the mongod.exe.
  • Alternatively, you can also navigate to the path where MongoDB is installed, for example, “C: MongoDB”
  • Then you need to navigate to the bin folder, locate the mongod.exe and double click the same to perform the execution.
  • You are also allowed to navigate to the required folder, for example, “C: MongoDB/bin” and type mongo to connect to MongoDB through the shell.

Q30) What is meant by the _ id Field in MongoDB?

In MongoDB, each document will be stored as a collection that will require a unique _id field which acts as a primary key. If an inserted document omits the _id field, the MongoDB driver will automatically generate an ObjectId for the _id field. The _id field includes the following behavior and constraints:

  • By default, MongoDB will be creating a unique index on the _id field during the creation of a collection.
  • The _id field will always be the first field in the documents. If the server receives a document that does not include the _id field first, then the server will move the field to the beginning.
  • The _id field may also contain values of any BSON data type, other than an array.

While storing values for _id, we can follow these options: Use an ObjectId,  or use a natural unique identifier, also generate an auto-incrementing number, generate a UUID in our application code. Most of the MongoDB driver clients will contain the _id field and will also generate an ObjectId before it sends the insert operation to MongoDB; however, if the client sends a document without an _id field, the MongoDB will add the _idfield and then generate the ObjectId.


31) What is MongoDB?
MongoDB is a high-performance, open-source, document-oriented NoSQL database that stores data in JSON-like BSON format. It is designed to handle unstructured data and offers dynamic schema capabilities.


32) What are the advantages of using MongoDB?
The following are the advantages of using MongoDB:

  1. a) Flexible and schema-less data model
  2. b) Horizontal scalability with sharding
  3. c) High performance due to memory-mapped storage engine
  4. d) Easy integration with various programming languages
  5. e) Rich query language support

33) How does MongoDB ensure high availability?
MongoDB ensures high availability through replica sets. A replica set consists of multiple nodes, including a primary and a secondary node. If and when the primary node fails, the secondary nodes will be automatically elected as the new primary.


34) Explain the concept of sharding in MongoDB.
Sharding is used to distribute data across multiple machines to achieve horizontal scalability. Each shard contains a subset of data, and MongoDB’s balancer ensures data distribution and load balancing across shards.


35) What is BSON?
BSON or Binary JSON is a binary representation of JSON documents used in MongoDB for efficient storage and data exchange.


36) How can you create a database in MongoDB?
To create a new database, use the use command followed by the database name. MongoDB creates the database when you insert data into it.


37) How do you insert documents into a collection in MongoDB?
You can insert documents into a collection using the insertOne() or insertMany() methods.


38) What is the difference between findOne() and find()?
findOne() returns a single document that matches the query, while find() returns a cursor to the documents that match the query.

findOne() returns null if no document matches the query, while find() returns an empty cursor.


39) How can you update a document in MongoDB?
You can update a document using the updateOne() or updateMany() methods.


40) Explain the Aggregation Pipeline in MongoDB.
The Aggregation Pipeline is a powerful tool for processing and transforming data in MongoDB. It consists of stages, each representing a data processing step. Documents are passed through the stages in sequence, allowing complex data manipulations.


41) What is the use of the $match stage in the Aggregation Pipeline?
The $match stage filters documents based on a specified condition, similar to the find() method.


42) How can you index data in MongoDB?
You can create indexes using the createIndex() method. Indexes improve query performance and help in optimising data retrieval.


43) What is a covered query in MongoDB?
A covered query is a type of query where all the queried fields and the fields to return are covered by an index, making the query faster.


44) Explain the concept of Time-to-Live (TTL) indexes. 
TTL indexes automatically delete documents after a certain period, specified in seconds.

db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })


45) How do you perform transactions in MongoDB? 
MongoDB supports multi-document transactions for replica sets and sharded clusters using the startSession() and commitTransaction() methods.


46) What is the use of the $project stage in the Aggregation Pipeline?
The $project stage allows you to reshape the documents by specifying which fields to include or exclude from the output.


47) Explain the $group stage in the Aggregation Pipeline.
The $group stage is used for grouping documents by a specified key and performing aggregate functions on the grouped data.


48) What is a compound index in MongoDB?
A compound index includes multiple fields, offering better query performance for complex queries.


49) How do you perform text search in MongoDB?
MongoDB supports text search using the $text operator.


50) Explain the purpose of the $unwind stage in the Aggregation Pipeline.
The $unwind stage is used to transform an array field into multiple documents, one for each array element.


51) How can you perform a case-insensitive search in MongoDB?
You can perform a case-insensitive search using regular expressions with the i option.


52) What is the use of the $sort stage in the Aggregation Pipeline?
The $sort stage is used to reorder the documents based on a specified field.


53) How do you delete documents from a collection in MongoDB? 
You can delete documents using the deleteOne() or deleteMany() methods.


54) Explain the concept of indexing strategies in MongoDB.
MongoDB offers various indexing strategies, including single-field indexes, compound indexes, multi-key indexes, and geospatial indexes, to optimise query performance for different use cases.


55) What is the use of the $limit stage in the Aggregation Pipeline?
The $limit stage restricts the number of documents passed to the next stage in the Aggregation Pipeline.


56) How do you perform a bulk write operation in MongoDB?
You can use the bulkWrite() method to perform multiple insert, update, or delete operations in a single request.


57) Explain the explain() method in MongoDB.
The explain() method provides information about how a query is executed, including details about the chosen index, execution time, and more. It helps in query optimisation.


58) What is the GridFS in MongoDB?
GridFS is a specification used for storing and retrieving large binary files, such as images or videos, in MongoDB.


59) How do you create a TTL index for specific documents in MongoDB?
You can create a TTL index with a filter to apply the expiration only to specific documents.


60) Explain the concept of capped collections in MongoDB.
Capped collections are fixed-size collections that automatically overwrite older documents when the collection reaches its maximum size. They are useful for storing logs and other time-sensitive data.


61) What are indexes in MongoDB, and why are they essential?
Indexes in MongoDB are data structures that improve the performance of queries by providing efficient access to data. They help speed up data retrieval and reduce the amount of data MongoDB needs to scan to find the matching documents.


62) How do you drop a collection in MongoDB?
You can drop a collection using the drop() method.

db.collection.drop()


63) Explain the concept of a covered query in MongoDB.
A covered query is a query where all the fields in the query are part of an index, and the index contains all the data needed to fulfil the query. As a result, MongoDB can return the results directly from the index without accessing the actual documents.


64) How can you perform a case-insensitive sort in MongoDB?
You can perform a case-insensitive sort using the $collation option.


65) What is the Aggregation Framework in MongoDB?
The Aggregation Framework is a powerful tool for processing and transforming data in MongoDB. It consists of pipeline stages that allow complex data manipulations, filtering, grouping, and more.


66) Explain the purpose of the $group stage in the Aggregation Framework.
The $group stage groups documents by a specified key and performs aggregate functions on the grouped data.


67) How do you perform a text search with stemming in MongoDB?
To perform a text search with stemming, use the $text operator along with the $language option.


68) What is the primary key in MongoDB, and can it be changed?
In MongoDB, the primary key is the _id field, and it uniquely identifies each document in a collection. Once set, the _id value is immutable and cannot be changed.


69) How can you create a compound index with a unique constraint in MongoDB?
You can create a compound index with a unique constraint by setting the unique option to true.


70) What are Secondary Indexes in MongoDB?
Secondary indexes in MongoDB are additional indexes created on fields other than the primary key. They allow for efficient querying on frequently accessed fields.


71) How can you limit the number of documents returned in a MongoDB query?
You can use the limit() method to restrict the number of documents returned in a query.


72) What is the purpose of the $skip stage in the Aggregation Framework?
The $skip stage is used to skip a specified number of documents in the Aggregation Pipeline.


73) How do you perform a case-sensitive search in MongoDB?
To perform a case-sensitive search, use a regular expression without the i option.


74) Explain the concept of Journaling in MongoDB.
Journaling in MongoDB is a feature that provides data durability by writing every write operation to a journal before applying it to the database. This ensures that data is not lost in the event of a crash.


75) How can you retrieve distinct values from a field in MongoDB?
You can use the distinct() method to retrieve distinct values from a field.


76) What is the difference between a NoSQL database and a relational database?
In a NoSQL database like MongoDB, data is stored in a non-tabular, flexible format, usually as JSON-like documents. NoSQL databases are horizontally scalable and better suited for handling unstructured data and large volumes. On the other hand, a relational database follows a tabular structure with predefined schemas and is suitable for structured data with complex relationships between tables.


77) How does MongoDB handle transactions?
MongoDB supports multi-document transactions for replica sets and sharded clusters. Transactions provide all-or-nothing semantics, allowing multiple operations to be grouped into a single transaction that either succeeds completely or fails entirely.


78) What is Map-Reduce in MongoDB?
Map-Reduce is a data processing paradigm used in MongoDB to perform complex data aggregations and transformations. It involves two stages: Map, where data is sorted and grouped based on a key, and Reduce, where the grouped data is aggregated based on the key.


79) How do you perform a case-insensitive text search with stemming in MongoDB?
To perform a case-insensitive text search with stemming, use the $text operator along with the $caseSensitive and $diacriticSensitive options.


80) Explain the concept of a database profiler in MongoDB.
The database profiler is a feature in MongoDB that allows you to record and analyse performance statistics for database operations. It helps in identifying slow queries and potential bottlenecks.


81) What is the use of the $graphLookup stage in the Aggregation Pipeline?
The $graphLookup stage performs a recursive search on a collection, allowing you to perform graph-based queries and retrieve connected data.


82) How do you backup and restore a MongoDB database?
You can backup a MongoDB database using the mongodump command and restore it using the mongorestore command.


83) What is the significance of the WiredTiger storage engine in MongoDB?
WiredTiger is the default storage engine in MongoDB since version 3.2. It offers significant improvements in performance, compression, and concurrency control, making MongoDB more efficient and scalable.


84) How can you retrieve the total count of documents in a collection in MongoDB?
You can use the countDocuments() method to retrieve the total count of documents in a collection.


85) Explain the concept of Change Streams in MongoDB.
Change Streams in MongoDB allow applications to listen for real-time changes in a collection. They provide a way to monitor data changes and trigger actions based on those changes.


86) How do you create an index on an array field in MongoDB?
To create an index on an array field, specify the field name inside quotes in the createIndex() method.


87) What is the $out stage in the Aggregation Framework used for?
The $out stage writes the output of an aggregation pipeline to a specified collection, effectively creating a new collection with the aggregated data.


88) How can you perform a case-insensitive sort with collation in MongoDB 4.0+?
MongoDB 4.0 introduced collation support directly in the sort() method.


89) Explain the difference between sharding and replication in MongoDB.
Sharding is the process of distributing data across multiple servers or shards to achieve horizontal scalability and handle large amounts of data. While replication is the process of synchronising data across multiple servers to ensure high availability and data redundancy. It involves creating multiple copies (replicas) of data on different nodes.


90) How can you find the size of a collection in MongoDB?
You can use the stats() method to find the size of a collection in MongoDB.

db.collection.stats().size


91) Explain the concept of compound documents in MongoDB.
Compound documents in MongoDB refer to nested or embedded documents within a document. This allows you to represent complex data structures and relationships without the need for joins as in traditional relational databases.


92) What is the purpose of the $redact stage in the Aggregation Framework?
The $redact stage allows you to apply access control rules to restrict the documents returned in the aggregation pipeline based on certain conditions.


93) How do you perform a full-text search with weights in MongoDB?
To perform a full-text search with weights, use the $text operator along with the $search option and the $meta projection to specify the weights for fields.


94) What are TTL indexes, and how do they work in MongoDB?
TTL (Time-to-Live) indexes in MongoDB are special indexes that automatically remove documents after a specified amount of time. They are commonly used for data that has an expiration date, such as temporary sessions or log entries.

Back to list

Leave a Reply

Your email address will not be published. Required fields are marked *