Mongo-DB

MongoDB is a document oriented database. It is an open source product, developed and supported by a company named 10gen. MongoDB is designed to make it easier for organizations to develop and run applications that provide high performance, high availability and easy scalability, and support a variety of data types.

MongoDB’s document data model lets developers easily store and combine data of any structure, without sacrificing data access or indexing functionality. This enables database administrators to dynamically modify the schema with no downtime.

What is Document oriented database?

It is nothing but a data structure with name-value pairs like in JSON. It is very easy to map any custom Object of any programming language with a MongoDB Document. For example: Book object has attribute title, author, publisher and edition, where edition is a List.

Document for Book in MongoDB will be like:

mongodb-database mongodb-json

We can see, Documents are actually a JSON representation of custom Objects. Also, excessive JOINS can be avoided by saving data in form of Arrays and Documents(Embedded) inside a Document.

When to go for MongoDB

First and foremost, MongoDB is not a replacement for any traditional RDBMS databases. But, considering the fact that the MongoDB structures/stores the data in the form of BSON (Binary representation of JSON) which is a self-explaining and human readable data format and the way it provides the scaling feature to the application to be developed, below are some important factors which clearly states that the developer(s) should go for the use of MongoDB for developing their applications.

  • Data Insert Consistency
  • Data Corruption Recovery
  • Load Balancing
  • Avoid JOINS
  • Best suited for changing schema
  • Not Relational data

MongoDB works on the concept of a document and collection.

Database:

A Database is a physical container for collections.
Each database gets its own set of files on the file system.
A single MongoDB server typically has multiple databases.

Document

In MongoDB, data or records are called documents and the documents are stored in a binary JSON (BSON) format.

Documents are further organized into collections. However, in the same collection, the schema of one document can be different from the other.

Collection

The Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database.

Collections do not enforce a schema.

Advantages of MongoDB over RDBMS

  • Schema less: MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another.
  • Structure of a single object is clear.
  • No complex joins.
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that’s nearly as powerful as SQL.
  • Ease of scale-out: MongoDB is easy to scale.
  • Uses internal memory for storing the working set, enabling faster access of data.

Why use MongoDB?

  • Document Oriented Storage − Data is stored in the form of JSON style documents.
  • Index on any attribute
  • Replication and high availability
  • Auto Sharding
  • Rich queries
  • Fast in-place updates

The following table shows the relationship of RDBMS terminology with MongoDB.

RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
Column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by MongoDB itself)

MongoDB supports many datatypes. Some of them are:

Data Type Description
String This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.
Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
Boolean This type is used to store a Boolean (true/ false) value.
Double This type is used to store floating point values.
Min/ Max keys This type is used to compare a value against the lowest and highest BSON elements.
Arrays This type is used to store arrays or list or multiple values into one key.
Arrays This type is used to store arrays or list or multiple values into one key.
Timestamp This can be handy for recording when a document has been modified or added.
Object This datatype is used for embedded documents.
Null This type is used to store a Null value
Symbol This datatype is used identically to a string; however, it’s generally reserved for languages that use a specific symbol type.

Security:

MongoDB has five core security areas

  1. Authentication

LDAP Authentication centralizes items in your company directory.

  1. Authorization

Authorization defines what role-based access controls the database provides.

  1. Encryption

Encryption can be broken into At-Rest and In-Transit. Encryption is critical to securing MongoDB.

  1. Auditing

Auditing refers to the ability to see who did what in the database.

  1. Governance

Governance refers to document validation and checking for sensitive data (such as an account number, password, Social Security number, or birth date). This refers to both knowing where sensitive data is stored and preventing sensitive data from being introduced into the system.

Disadvantages:

  • Do not support transactions
  • Do not support join operation

Conclusion:

MongoDB is the best fit if you have a lot of data, but the relation between them is weak, for example, a stream of independent events, actually the earlier application of MongoDB is to record online ads clicking statistical information. It’s not fit for strongly related data like, your bank account information, whenever you transfer money, many related bank data needs to be modified at the same time, and they either all done, or nothing is done. RDBMS does this by transaction.