Pages

January 27, 2020

#MongoDB Part 1: Interview Questions And Answers

What 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. Documents consist of key-value pairs which are the basic unit of data in MongoDB. Collections contain sets of documents and function which is the equivalent of relational database tables.

What are the features of MongoDB?
  • Each database contains collections which in turn contains documents. Each document can be different with a varying number of fields. The size and content of each document can be different from each other.
  • The document structure is more in line with how developers construct their classes and objects in their respective programming languages. Developers will often say that their classes are not rows and columns but have a clear structure with key-value pairs.
  • The rows (or documents as called in MongoDB) doesn't need to have a schema defined beforehand. Instead, the fields can be created on the fly.
  • The data model available within MongoDB allows you to represent hierarchical relationships, to store arrays, and other more complex structures more easily.
MongoDB Example
The below example shows how a document can be modeled in MongoDB.
{
   _id: < ObjectId > ,
   CustomerName:"Christien",
   Order:{
      ObjectId:999,
  ProductCode:LAP_OO,
  Quantity:7
   }
}

In the above example, the _id field is added by MongoDB to uniquely identify the document in the collection.

In case of RDBMS the Order details (order id, product code etc) is usually stored in a separate table, however in MongoDB it is actually stored as an embedded document in the collection itself.

What are the key components of MongoDB Architecture?
_id: This field represents a unique value in the MongoDB document. The _id field is like the document's primary key. If you create a new document without an _id field, MongoDB will automatically create it for us.

Collection: This is a grouping of MongoDB documents. A collection is the equivalent of a table which is created in any other RDMS such as Oracle or MS SQL. A collection exists within a single database.

Cursor: This is a pointer to the result set of a query. Clients can iterate through a cursor to retrieve results.

Database: This is a container for collections like in RDMS wherein it is a container for tables. Each database gets its own set of files on the file system. A MongoDB server can store multiple databases.

Document: A record in a MongoDB collection is basically called a document. The document, in turn, will consist of field name and values.

Field: A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in relational databases.

JSON: This is known as JavaScript Object Notation. This is a human-readable, plain text format for expressing structured data. JSON is currently supported in many programming languages.

Why are the advantages of using MongoDB?

  • Document Oriented: Since MongoDB is a NoSQL type database, instead of having data in a relational type format, it stores the data in documents. Because of this MongoDB is quite flexible and adaptable to real business world situation and requirements.
  • Adhoc queries: MongoDB supports searching by field, range queries, and also regular expression searches. Queries can be made to return specific fields within documents.
  • Indexing: To improve the search performance Indexes can be created within MongoDB. Any field in a MongoDB document can be indexed.
  • Load balancing: MongoDB uses the concept of sharding to scale horizontally by splitting data across multiple MongoDB instances. MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure.
  • Replication: Because of replica sets MongoDB can provide high availability. A replica set consists of two or more MongoDB instances. Each replica set member may act in the role of the primary or secondary replica at any time. The primary replica is the main server which interacts with the client and performs all the read/write operations. The Secondary replicas maintain a copy of the data of the primary using built-in replication. When a primary replica fails, the replica set automatically switches over to the secondary and then it becomes the primary server.

What is the difference between MongoDB and RDBMS?

  1. In RDBMS, the table contains the columns and rows which are used to store the data whereas, in MongoDB, this same structure is known as a collection. The collection contains documents which in turn contains Fields, which in turn are key-value pairs.
  2. In RDBMS, the row represents a single, implicitly structured data item in a table. In MongoDB, the data is stored in documents.
  3. In RDBMS, the column denotes a set of data values. These in MongoDB are known as Fields.
  4. In RDBMS, data is sometimes spread across various tables and in order to show a complete view of all data, a join is sometimes formed across tables to get the data. In MongoDB, the data is normally stored in a single collection, but separated by using Embedded documents. So there is no concept of joins in MongoDB.

-K Himaanshu Shuklaa..

No comments:

Post a Comment