Monday, October 2, 2017

MongoDB Overview

MongoDB is a free and open-source, cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document, in simple terms JSON (JavaScript Object Notation) format at high level and at low level it’s BSON (Binary JSON). Before we understand we need to understand what it means when we say MongoDB works on concept of collection and document. In this article I’ll try to help you out with setup and basic commands for MongoDB. Let’s begin with understanding what makes MongoDB.

ACID Database Transactions
ACID (Atomicity, Consistency, Isolation and Durability) is a set of properties of database transactions intended to guarantee validity even in the event of errors, power failures, etc. We have used many RDBM databases designed on ACID properties.

CAP Theorem
CAP theorem states that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees: Consistency. Availability. Partition tolerance.



Based combination of consistency, availability and partition tolerance new generation of databases are designed. MongoDB is designed considering consistency and partition tolerance. When we want scalable NoSQL database MongoDB stands very clear in choice.


MongoDB:
MongoDB works on concept of collection and document. We need to understand following basic terms before we start MongoDB

  • Database: 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.
  • Collection: 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. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.
  • Document: A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

MongoDB uses Sharding to be partition tolerance it’s the process of storing data records across multiple machines and it is MongoDB's approach to meeting the demands of data growth.

MongoDB Installation:
You can download setup from https://www.mongodb.com/ or opt for MongoDB Atlas, it allows to Deploy, operate, and scale a MongoDB database in the cloud.  Basically database as a Service. Latest version of MongoDB DB is 3.4 at this time... Once you downloaded setup mongodb-win32-x86_64-enterprise-windows-64-3.4.9-signed.msi, simply next clicks will install server and client in your machine




In Bin folder you will get Database Server and Client which is mongod.exe and mongo.exe.

These are exes which will allow you to run shell version of MongoDB.
 --dbpath parameter in MongoDB will allow you to control what directory MongoDB reads and writes its data from.

 

Once data path is configured you are good to go.

MongoDB collection and document:
·         To make collection you need to run db.createCollection command e.g.

>db.createCollection("Products")
{ "ok" : 1 }
·         The basic syntax of insert() command is db.COLLECTION_NAME.insert(document)
>db.Products.insert({itemName: “Pen”, QOH: 1000,Price:5.00 })
For multiple insert you need to pass JSON array... Any document inserted into MongoDB get assigned _id for indexing which you can use while fetching or searching records.

·         The basic syntax of find() command is db.COLLECTION_NAME.find(document)
>db.Products.find()
Similar way you can update and other commands.Refer https://docs.mongodb.com/manual/reference/mongo-shell/

Additional tool
Robo 3T is the free lightweight GUI for MongoDB. It’s formerly known as Robomongo and can be downloaded from https://robomongo.org/


Advantages of MongoDB over RDBMS:
1) 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.Conversion/mapping of application objects to database objects not needed.
Uses internal memory for storing the (windowed) working set, enabling faster access of data.

This is to begin with MongoDB...What do you think? Let me know your suggestions and inputs.



Courtesy: Several online blogs & other resources