Introduction to MongoDB
krati
21 Jun 2016 01:02 PM
The most innovative structures for storing data today are NoSQL and object-oriented databases. These do not follow the table/row/column approach of RDBMS. There are four types of NoSQL databases :-
A document is stored in key value pair in mongoDB. MongoDB saves documents in BSON (Binary JSON) form and maximum size of any document can be 16 megabytes. Let’s take an example of Student Document: { “_id” : ObjectId(7df78ad8902c), “name” : “xyz”, “emailId” : “xyz@abc.com”, “phoneNo” : NumberLong(9999999999), “address” : { “permanentAddress” : “New Delhi-110011”, "correspondenceAddress" : “New Delhi-110011”, }, “class” : “8th”, “rollNumber” : 201, “subjects” : } In the above document first field is“_id” which is 12 bytes hexadecimal number generated by mongoDB itself, provides uniqueness to every document. Advantages of MongoDB
- Document Database
- Graph Database
- Key-value Database
- Wide-column Database
- a 4-byte value representing the seconds since the Unix epoch,
- a 3-byte machine identifier,
- a 2-byte process id
- a 3-byte counter, starting with a random value.
- Reference Data Model - This data model stores the relationship between data by including references from one document to another. These are normalized data Models.
- Embedded Data Model - This data model capture relationships between data by storing related data in a single document structure.These are denormalized data models which allow applications to retrieve and manipulate related data in a single database operation.
| RDBMS | MONGODB |
| Table | Collection |
| Row | Document |
| Column | Field |
| Table Join | Embedded Document |
- MongoDB stores data in forms of Json kind of documents which are very flexible. There is no need to define schema for storing the documents. In a single document we can store multiple type of data. As we can see the same in our above example.
- MongoDB provides embedded Document structure which reduce the need to perform complex join queries.
- It provides high scalability.
- MongoDB provides auto-Sharding and replication which increases the query performance.
- It supports multiple storage engines.
- Shards - A shard is a replica set that contains a subset of the data for the sharded cluster.Typically each shard is a replica set. The replica set provides high availability and data consistency for the data in each shard.
- Query Routers - MongoDb query routers route queries and write operations to shards in sharded cluster.It provides an interface to the Applications to commute with the shards through sharded cluster.
- Config servers - Config servers store the cluster’s metadata.The query router uses this metadata to target operations to specific shards.This data contains a mapping of the cluster’s data set to the shards.
- WiredTiger Storage Engine - It is the default storage engine starting in MongoDB 3.2.WiredTiger uses document-level concurrency control for write operations. As a result, multiple clients can modify different documents of a collection at the same time.
- MMAPv1 Storage Engine - It is the default storage engine for MongoDB versions before 3.2.MMAPv1 is MongoDB’s original storage engine based on memory mapped files. It excels at workloads with high volume inserts, reads, and in-place updates.
- In-Memory Storage Engine - The In-Memory Storage Engine is available in MongoDB Enterprise.Rather than storing documents on-disk, it retains them in-memory for more predictable data latencies. Other than some metadata and diagnostic data, the in-memory storage engine does not maintain any on-disk data, including configuration data, indexes, user credentials, etc.