Mongodb: A complete guide about MongoDB, MongoDB with Node JS, Mongodb Replication and shading concept

Tuesday, December 24, 2019

Mongodb: A complete guide about MongoDB, MongoDB with Node JS, Mongodb Replication and shading concept

MongoDB is a document-oriented NoSQL database used for high volume data storage. MongoDB is a database which came into light around the mid-2000s, It falls under the category of a NoSQL database.  

Mongo DB

1. Overview of MongoDB

Developed By -  MongoDB INC, 11 Feb 2009.

Features - 

  1. Queries: It supports ad-hoc queries and document-based queries.
  2. Index Support: Any field in the document can be indexed.
  3. Replication: It supports Master-Slave replication. MongoDB uses a native application
  4. to maintain multiple copies of data. Preventing database downtime is one of the replica set’s features as it has self-healing shards.
  1. Multiple Servers: The database can run over multiple servers. Data is duplicated to 
  2. foolproof the system in the case of hardware failure.
  3. Auto-shading: This process distributes data across multiple physical partitions 
  4. called shards. Due to shading, MongoDB has an automatic load balancing feature.
  5. MapReduce: It supports MapReduce and flexible aggregation tools.
  6. Failure Handling: In MongoDB, it’s easy to cope with cases of failures. 
  7. Huge numbers of replicas give out increased protection and data availability 
  8. against database downtime like rack failures, multiple machine failures, and 
  9. data center failures, or even network partitions.
  10. GridFS: Without complicating your stack, any sizes of files can be stored. 
  11. GridFS feature divides files into smaller parts and stores them as separate documents.
  12. Schema-less Database: It is a schema-less database written in C++.
  13. Document-oriented Storage: It uses BSON format which is a JSON-like format.
  14. Procedures: MongoDB JavaScript works well as the database 
  15. uses the language instead of procedures.

2. What is MongoDB?

  • MongoDB is a document-oriented database that stores data in JSON-like documents with the dynamic schema. It means you can store your records without worrying about the data structure such as the number of fields or types of fields to store values. MongoDB documents are similar to JSON objects.
  • MongoDB is one of the most popular open-source NoSQL database written in C++. As of February 2015, MongoDB is the fourth most popular database management system.

3. Right Audience to Learn MongoDB

  • Beginners - Basic Computer Background | Science Background | Arts Background
  • Bachelor of Computer Application - Background Students
  • Master of Computer Application - Background Students
  • PHD in Computer Science - Background Students
  • Data Analyst - Any Experience level
  • Web Developer - Any technology
  • Full Stack Engineer - Any Experience level
  • Full Stack Technical Architect
  • Web Analyst

4. How will this technology help you in your career growth 2019-2020?

Popularity of MongoDB
Popularity of MongoDB

5. How MongoDB Documents looks like ?

MongoDB Documents Looks Like
MongoDB Documents Looks Like

6. How MongoDB stores the data?

As we know, RDMS stores data in tables format and uses structured query language (SQL) to query the database. RDBMS also has pre-defined database schema based on the requirements and a set of rules to define the relationships between fields in tables.

But MongoDB stores data in documents format in-spite of tables. We can change the structure of records (which is called as documents in MongoDB) simply by adding new fields or deleting existing ones. This ability of MongoDB helps us to represent hierarchical relationships, to store arrays, and other more complex structures easily. MongoDB provides high performance, high availability, easy scalability, and out-of-the-box replication and auto-shading.

How MongoDB Store the Data
How MongoDB Store the Data

7. Why and Where we need to use MongoDB?

Since MongoDB is a NoSQL database, so we need to understand when and why we need to use this type of database in real-life applications. Like when we want to deal with large numbers of databases like we want to insert thousand of record in a second then MongoDB is the best choice.

  • MongoDB is good for the E-Commerce type of project, also its good for blog and content management.
  • Social Networking Site.
  • Oil industry, where we are having the scenario to deal with large number of database.

8. MongoDB Supports Languages

  • C, C++, C#, .Dot net

  • Java, PHP, Node JS

  • It also supports perl python and Rubi.

    9. Popular Company's are using MongoDB



    More Details please watch the below video link -

    10. Installation of MongoDB and RobotT for MongoDB data visualization

    More Details please watch the below official link -

    Download Code EditorVisual Studio -

    Node JS for MAC/Windows/Linux/Ubuntu 

    Details please watch below video - 

    11. Inserting data into MongoDB

    Insert One
    for(var i =1;i<=10000;i++){db.employee.insertOne(   { emp_id:i, name: "atique", age: i+10,dept:”IT”, skills: ["nodejs", “angular”,”mongodb”,”ElasticSearch”], leaves: { CL: 10, SL: 10, PL: 21 } })}

    Insert Many
       { id:”120”, name: "sunny", dept:”Testing”, skills: ["jira", “mentis bug tracking tool”], leaves: { CL: 10, SL: 10, PL: 21 } },
       { id:”122”, name: "sonu", dept:”IT”, skills: ["nodejs", “angular”,”mongodb”,”ElasticSearch”], leaves: { CL: 10, SL: 10, PL: 21 } },
       { id:”120”, name: "sunny", dept:”Testing”, skills: ["jira", “mentis bug tracking tool”], leaves: { CL: 10, SL: 10, PL: 21 } },
       { id:”122”, name: "sonu", dept:”IT”, skills: ["nodejs", “angular”,”mongodb”,”ElasticSearch”], leaves: { CL: 10, SL: 10, PL: 21 } },
       { id:”123”, name: "rubi", dept:”IT frontend”, skills: ["html", “css”,”angularjs”,”vue js”], leaves: { CL: 10, SL: 10, PL: 21 } }
       { id:”123”, name: "rubi", dept:”IT frontend”, skills: ["html", “css”,”angularjs”,”vue js”], leaves: { CL: 10, SL: 10, PL: 21 } }

    12. Updating data into MongoDB

    db.student.update({"empid" : 42},{$set: { "dept" : "HR"}}); )

    Update one

    db.student.updateOne({'dept':'HRA'}, {$set: {'dept':'IT'}})

    Update Many

    db.student.updateMany({'dept':'HR'}, {$set: {'dept':IT}})

    Replace One

    db.student.replaceOne({'dept':'business'}, {'dept':'sale'})

    13. Deleting data from MongoDB

    Delete One


    Delete Many



    Details please watch below video -  

    14. MongoDB Projection, Find, And Or

    Projection or Find

    db.getCollection('customercomplaints').find( { “email”:””} ).forEach(printjson);


    db.getCollection('employee').find({ $or: [    { age: { $gt: 40 }},    { emp_id: { $gt: 10}}    ]  }).count()


    db.getCollection('employee').find({ $and: [ { age: { $gt: 40 }},
    {emp_id: { $gt: 10 }} ] }.count()

    More Details & practical example please watch the below video link -

    15. MongoDB Sort, Count, Skip & Limit


    db.getCollection('customercomplaints').find().sort({'email':-1}) //Descending Order



    Skip and Limit


    More Details and practical example please visit the below video link - 

    16. What is MongoDB Indexing?

    Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and requires MongoDB to process a large volume of data.


    for(var i=1;i<=500000;i++) {
    db.employee.insert({'emp_id':NumberInt(i+1), age: NumberInt(i+10), dept: "IT", Skills: ["Nodejs","Angular7","Elasticsearch"])

    Ensure Index


    Drop Index


    More Details & practical example please watch the below video link -

    17. What is MongoDB Aggregation?

    Aggregations groups values from multiple documents, and get performed variety of operation on the group data. And it’s going to be return the single the result after the aggregations.  So if you are sql user, or you use the sql earlier you might have notice, we are using select (*) from table_Name. So this * is for aggregation. And this count will return you the number of rows. Same way aggregation functions works in Mongo[{$group : {_id : "$gender", myreport: {$sum: NumberInt(1)}}}])[{$group : {_id : "$gender", myreport: {$max: "$age"}}}])[{$group : {_id : "$gender", myreport: {$min: "$age"}}}])

    More Details & practical example please watch the below video link -

    18. What is MongoDB Replication?

    Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different
    database servers.

    Also, Replication protects a database from the loss of a single server.

    19. How MongoDB Replication perform?

    Mongodb Replication
    Mongodb Replication

    In this diagram, we are having multiple client applications, So whenever any client is doing write operation, that means any updation or deletion. 

    Then the updated data must convey from the primary database to the Replica Set. Means data has to be synchronized with primary database.

    Mongodb Replication
    Mongodb Replication

    So there are multiple secondary nodes are there, and only 1 primary node. So there is a heartbeat signal which is generated from the primary node and connected to the secondary node.  So absences of the heartbeat signal, due to some cost primary database is gone done. So what will happens next. You can see my next diagram. So here among of the secondary node decided and made 1 as a primary node.

    More Details & practical example please watch the below video link -

    20. What is MongoDB Sharding ?

    As the size of the data increases, a single machine may not be sufficient to store the data nor provide an acceptable read and write throughput. Sharding solves the problem with horizontal scaling. With sharding, we can add more machines to support data growth and the demands of read and write operations.

    In more convenient way I can say that, Sharding is the process of storing data records across multiple machines and it is MongoDB's approach to meeting the demands of data growth.

    Mongodb sharding
    Mongodb sharding

    So here, each and every shard is the part of replica set. Where 1 is primary node and 2 is secondary node. And query Router is nothing but here mongoS, which comes as a driver while we are installing Mongo DB. It will take care about data distribution. 
    If you have not watch my previous video I strongly recommended you to go and watch my previous video, Mongo DB replication tutorial 8. 

    21. How MongoDB Sharding works?

    How Mongodb sharding Works
    How Mongodb sharding Works

    Into more convenient way,  I have draw a diagram for your better understanding, So here you can see that, s1, s2, s3 nothing but shared. Left hand side circle is replica set. And app is connected to shared via mongos. Ok, So let’s take a example of order table. Say for example, I want to find a order id which range is 450, So the request go to mongoS via app. There is a shard key, which tells you that range can be present in what shared? Shared s1, s2 or s3, s4 or s5. Based on range based sorting and shared key,  it will make a query and give you result. Alright…..

    Okay. So now we will go do setup in our local.

    More Details & practical example please watch the below video link -

    22. What MongoDB Backup and Restore?

    # Mongodb Backup and Restore

    Go to your terminal -- >

    1. mongod
    2. Open robotT to check the connection

    # Mongodb dumping entire all database and Restore

    1. mongodump ---- for dumping the database
    2. delete all the database from roboT
    3. mongorestore --- for restoring the database

    # Mongodb dumping single database and Restore

    1. mongodump --db mongodbTutorial
    2. delete database mongodb Tutorail from RoboT || db.databaseName.drop()
    3. mongorestore

    # Mongodb dumping single collection and Restore single collection

    1. mongodump --db mongodbTutorial --collection school
    2. delete the collection from the perticular database from roboT || db.collectionName.drop()
    3. mongorestore --db mongodbTutorial --collection school dump/mongodbTutorial/school.bson

    More Details & practical example please watch the below video link -

    23. MongoDB with Node JS Followed by MVC Architecture?

    Connect Mongo DB with Node JS


    const mongoose = require('mongoose')

    const config=require('config');
    mongoose.connect(config.mongodb.url+''+config.mongodb.local_server_ip+':'+config.mongodb.port+'/'+config.mongodb.databaseName,{ useNewUrlParser:true, useUnifiedTopology: true },(err) => {if(!err){console.log('MongoDB connected...')} else{console.log('Error in DB connection: ' + JSON.stringify(err, undefined, 2) );}})
    module.exports = mongoose

    More Details & practical example please watch the below video link -