05th
May
MongoDB Interview Questions

MongoDB Interview Questions

  • Nikita Singh
  • 05th May, 2021
  • 750 Followers

About MongoDB

MongoDB is an open source database management system (DBMS) that uses a document-oriented database model which supports various forms of data. MongoDB totally changes how data is queried, stored, and retrieved from usual database systems. MongoDB is a single-model document database. MongoDB, on the other hand, doesn’t maintain these relationships. It’s a non-relational database, and everything you want to know about a record wants to be contained within that record itself. MongoDB is a also single-model document database.

MongoDB Interview Questions

1) Explain what is MongoDB?

MongoDB is an open-source database management system (DBMS) that uses a document-oriented database model which supports various forms of data. Instead of using tables and rows as in relational databases, MongoDB architecture is made up of collections and documents.

2) List some important features of MongoDB?

Some important features of MongoDB are

  • Support ad hoc queries
  • Supports Indexing & Replication
  • Load balancing
  • It provides high performance.
  • Easy to administer in the case of failures.
  • JSON data model with dynamic schemas
  • Auto-sharding for horizontal scalability
  • Built-in replication for high availability
  • Schema-less database
  • Uses JavaScript instead of Procedures.

3) What is namespace in MongoDB?

MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the collection. The concatenation of the collection name and database name is called a namespace.

4) What is BSON in MongoDB?

BSON is the binary encoding of JSON-like documents that MongoDB uses when storing documents in collections. It adds support for data types like Date and binary that aren't supported in JSON.

5) What type of DBMS is MongoDB?

MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents.

6) What is the document structure of MongoDB?

MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. MongoDB documents are composed of field-and-value pairs and have the following structure:

{
   field1: value1,
   field2: value2,
   field3: value3,
   ...
   fieldN: valueN
}

The value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents.

7) What is replica set in MongoDB?

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability and are the basis for all production deployments.

8) What is profiler in MongoDB?

MongoDB Profiler is a built-in tool that gives you the actual query level insights. It allows you to analyze all the queries which are being run by the database system.

9) Write the syntax for creating and droping a collection in MongoDB?

The creation of the collection is the basic step in MongoDB. Here is the syntax for creating a collection in MongoDB-

Db.createCollection (name, options)

Dropping a collection basically means deleting a collection. Following is the syntax for dropping a collection in MongoDB-

Db.collection.drop()

10) What is the size limit of a BSON Document?

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use an excessive amount of RAM or, during transmission, an excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

11) What is _id Field in MongoDB?

The _id field is the unique naming convention that MongoDB uses across all of its content. In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. By default, MongoDB creates a unique index on the _id field during the creation of a collection. The _id field is always the first field in the documents.

12) Explain what is ObjectId in MongoDB?

An ObjectId is a 12 byte binary BSON type that contains any 12 bytes you want. MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document.

13) What is replication and what are the primary and secondary replica sets?

Replication means synchronizing the data across multiple servers. It increases data availability. If a single server is lost, data is still intact in the other servers.

  • Primary replica set – MongoDB writes data only to primary or master replica set.
  • Secondary replica set – secondary or slave nodes can accept only reads. They replicate from the primary.

14) What is a collection in MongoDB?

A collection is a grouping of MongoDB documents. It is the equivalent of an RDBMS table. All documents in MongoDB are stored in collections.

15) What is Mongo shell?

Mongo shell is a JavaScript interface to MongoDB that can be used to query and update data. It is interactive and can also be used to do administrative operations.

16) What is sharding in MongoDB?

Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.

17) What is writeConcern in MongoDB?

As per MongoDB's documentation write concern is efined as "the level of acknowledgment requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters." Simply put, a write concern is an indication of 'durability' passed along with write operations to MongoDB.

18) What is use of upsert in MongoDB?

MongoDB upsert option is used with the update method which creates a new document if the query does not retrieve any documents satisfying the criteria.

Upsert Example

db.collection.update(query, update, {upsert: true})

19) Explain what is Mongoose?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.

20) List some alternatives of MongoDB?

RethinkDB, OrientDB, CouchDB, ArangoDB, PostgreSQL, Cassandra, IBM Cloudant, Cockroach DB are few alternatives of MongoDB.

21) What is journal in MongoDB?

MongoDB uses an on-disk journal to guarantee write operations and provide crash resiliency. MongoDB also creates a journal for every write that includes the exact disk location and the bytes that changed in the write.

22) What is __v field in Mongoose?

The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero.

23) What is Projection Operators? List some Projection Operators available in MongoDB?

Projection Operator is a unary operator in relational algebra that performs a projection operation. It displays the columns of a relation or table based on the specified attributes.

Projection Operators available in MongoDB are

  • $ Projects the first element in an array that matches the query condition.
  • $elemMatch Projects the first element in an array that matches the specified $elemMatch condition.
  • $meta Projects the document’s score assigned during $text operation.
  • $slice Limits the number of elements projected from an array. Supports skip and limit slices.

24) What is Bitwise Query Operator in MongoDB?

A bitwise operator operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations.

Bitwise Query Operator in MongoDB are

  • $bitsAllSet - matches if the input bit positions are all 1
  • $bitsAllClear - matches if the input bit positions are all 0
  • $bitsAnySet - matches if any of the bit positions are 1
  • $bitsAnyClear - matches if any of the bit positions are 0

25) Explain the structure of ObjectID in MongoDB?

An ObjectId in MongoDB is a 12-byte BSON type. In the 12-byte structure, the first 4 bytes of the ObjectId represents the time in seconds since the UNIX epoch. The next 3 bytes of the ObjectId represent the machine identifier. The next 2 bytes of the ObjectId represents the process ID.

26) What are Indexes in MongoDB?

An index in MongoDB is a special data structure that holds the data of a few fields of documents on which the index is created. Indexes improve the speed of search operations in the database.

MongoDB uses multikey indexes to index the content stored in arrays. Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement

27) What is a covered query in MongoDB?

A covered query in MongoDB is a query that can be satisfied entirely using an index and does not have to examine any documents. An index covers a query when all of the following apply:

  • all the fields in the query are part of an index, and
  • all the fields returned in the results are in the same index.
  • no fields in the query are equal to null (i.e. {"field" : null} or {"field" : {$eq : null}} ).

28) What is Aggregation in MongoDB?

Aggregation in MongoDB is nothing but an operation used to process the data that returns the computed results. Aggregation basically groups the data from multiple documents and operates in many ways on those grouped data in order to return one combined result.

29) What is Oplog in MongoDB?

The Oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases. MongoDB applies database operations on the primary and then records the operations on the primary's oplog.

30) What is Replication in MongoDB?

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server.

31) What is a Storage Engine in MongoDB?

A storage engine is the component of the database that is responsible for managing how data is stored, both in memory and on disk. MongoDB supports multiple storage engines, as different engines perform better for specific workloads. Choosing the appropriate storage engine for your use case can significantly impact the performance of your applications.

WiredTiger Storage Engine is the default storage engine in MongoDB.

32) Explain what is GridFS?

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB. Instead of storing a file in a single document, GridFS divides the file into parts, or chunks, and stores each chunk as a separate document.

33) What is use of capped collection in MongoDB?

Capped collections are fixed-size collections. These support high-throughput operations that insert and retrieve documents based on insertion order. Once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

Capped collections have the _id field and its default index. Some features specific to the capped collection:

  • The document size cannot change in an update or replacement operation.
  • Documents cannot be deleted (other than the automatic drop and create process).
  • The inserting order is preserved, so the most recently inserted documents can be retrieved efficiently.

34) What is MongoDB Atlas?

MongoDB Atlas is a fully-managed cloud database developed by the same people that build MongoDB. Atlas handles all the complexity of deploying, managing, and healing your deployments on the cloud service provider of your choice (AWS, Azure, and GCP).

35) What is splitting in mongodb?

MongoDB uses the shard key associated to the collection to partition the data into chunks. A chunk consists of a subset of sharded data. Each chunk has an inclusive lower and exclusive upper range based on the shard key.

Splitting in MongoDB is a process that keeps chunks from growing too large. When a chunk grows beyond a specified chunk size, or if the number of documents in the chunk exceeds the Maximum Number of Documents Per Chunk to Migrate, MongoDB splits the chunk based on the shard key values the chunk represent.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.