Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

MongoDB Interview Questions and Answers

|| 0

MongoDB Interview Questions and Answers
Stechies

What is MongoDB and why to use MongoDB?

MongoDB is an open source, object-oriented, scalable and powerful that uses a non-structured query language called NoSQL. As it is based on the NoSQL store model, instead of the traditional columns, the data is stored in documents in a collection.

The basic unit is in the form of a set of key-value pairs. MongoDB is an ideal tool for e-commerce management, blogging, and real-time analytics.

What are some key features of MongoDB?

Some features of MongoDB are:

  • Open source
  • Scalability
  • Automatic load balancing
  • It does not have a schema and is more flexible than other database systems.
  • It supports data replication and spreads the data over multiple systems.
  • It can be used as a file storage system, as Grid File System is included in MongoDB.

What is BSON in MongoDB?

BSON in MongoDB is Binary JSON is a binary coded version of JSON documents that MongoDB uses for storing documents. It supports data types for date and binary which is not present in JSON. Encoding and decoding are easier using BSON for different languages.

What are the types of data MongoDB store?

Data types supported by MongoDB are:

  • Numbers
  • Min key
  • Null
  • Object
  • Object ID
  • Date and Timestamp
  • Array
  • Regular expression
  • Binary data
  • String, Symbol
  • Max key
  • Boolean

Where is MongoDB data stored?

The data in MongoDB is stored on the disk as BSON files in the user's path directory that is /data/db. The storage engine helps manage the data in MongoDB.

How to check MongoDB version?

In Windows if the MongoDB pat is set beforehand, in the command line type the following code to check the version of MongoDB:

MongoDB --version

For checking MongoDB shell version:

mongo --version

What is sharding in MongoDB?

Sharding in MongoDB is the way of storing data records distributed over many systems. As the data size increases, a single machine may not be enough for proper read and write operations.

Sharding allows horizontal scaling by supporting the addition of multiple systems for handling the data load. It is a cost-effective approach as vertical scaling is very expensive.

How to create a collection in MongoDB?

In MongoDB, a collection can be created using the db.createCollection() method.

The basic syntax of the method is:

db.createCollection(name, options)
  • Where the name specifies the name of the collection that will be created. 
  • The options specifies the document having the configuration details of the collection. This is an optional parameter.
  • Other optional parameters are capped, size, autoIndexId, and max.

What is a replica set in MongoDB?

MongoDB performs replication through a replica set. It is a set of Mongod instances that host similar data. Here the primary node gets all the write operations and records all the operations inside a document called Oplog. The other nodes are called secondary and they replicate the primary node’s Oplog to perform operations on their data sets.

What are MongoDB indexes?    

MongoDB indexes are data structures that hold a part of the collection’s data in a way that can be traversed easily. The values of particular fields or fieldsets are stored by these indexes. Using indexes, queries are executed more efficiently and faster as the entire data set can be traversed easily. Large volumes of data can be scanned better.

How do MongoDB indexes work?

Indexes in MongoDB use the values of particular fields or fieldsets to find the requested document without searching the entire collection of data. Based on the criteria mentioned in the query, MongoDB puts a limit on the search.

According to the order of the value, the field values are stored. The sequential arrangement of indexes is used to sort and return the query results.

How MongoDB is scalable?

MongoDB is scalable as it supports horizontal scalability using the process of Sharding. This allows the administrators to increase the system capacity easily by adding the required number of machines.

Additional instances are added through Sharding to reduce the load while scanning data. It happens at the collection level, so the data is distributed over the servers.

What is ObjectID in MongoDB?

ObjectID is a BSON data type that has 12 bytes that has the following structure:

  • 4 bytes depicting seconds since UNIX epoch.
  • 3 bytes after that are for the machine identifiers.
  • 2 bytes after that are for the process id.
  • The last 3 bytes can contain some random values.

In the _id field of each document, MongoDB uses these ObjectID.

What is MongoDB compass?

MongoDB compass is a GUI based tool that allows the users to view the MongoDB data without using any query language. Any user can analyze the data and ad-hoc queries without having knowledge of query languages like NoSQL. You can easily explore the datasets and understand the query performances.

What is Scaling in MongoDB?

As the data size increases, a single system may not be enough for handling the read and write operations. MongoDB scales through horizontal and vertical methods.

There are two types of Scaling in MongoDB

  • Horizontal Scaling
  • Vertical scaling 

What horizontal scaling in MongoDB?

Horizontal scaling in MongoDB is achieved through the process of Sharding. As the data size can increase, a single machine may not be sufficient for all the read and write operations.

Sharding allows horizontal scaling by addition of multiple systems for handling the data load. The data is thus distributed over many systems which reduces the load on a single machine.

What is vertical scaling?

Vertical scaling in MongoDB is the process of increasing the capability of a server by adding more storage space, adding more RAM and opting for a more powerful CPU to handle data tasks. The process is not very cost effective and the systems may become difficult to maintain.

What is Pretty method in MongoDB?

The pretty() method is used to display the results of a query in a formatted manner. This method specifies the cursor object to show the results in a way that is easy to understand.

The command has the following syntax:

db.collection_name.find (<query_string>).pretty()

How to use GridFS in MongoDB?                  

GridFS is a file system used in MongoDB which is used to store and receive image, video and audio files. The data in these files are stored in MongoDB collections.

This functionality divides the files into chunks of data in separate documents. GridFS uses fs.files and fs.chunks to store these data chunks.

What is __v in MongoDB?

Each document in MongoDB has a property called versionKey. This has an internal document revision. The default document is __v.

What are some alternatives to MongoDB?

Some alternatives to MongoDB are CouchDB, Cassandra, Redis, HBase, and MySQL.

What is the comment syntax for MongoDB?    

The $comment operator allows the comments to be added along with a query. The following is the syntax for using a $comment:

db.collection.find( { <query> } ).comment( <comment> )

 


Advertisement
Advertisement
Advertisement