MONGO DB
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
What is a Database?
• A database is an organized collection of data stored and accessed electronically.
• More formally, a "database" refers to a set of related data and the way it is organized.
• The sizes of databases vary greatly.
• Small databases can be stored on a file system, while large databases are hosted on cloud systems.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Some Examples…
• The contact list in your phone.
• Students’ record in your college / university.
• Grocery Store Inventory.
• And so many more…..
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Types of Databases…(1)
• Relational Databases:
• Came into existence in 1960s, became dominant in the 1990s.
• Popular examples: IBM Db2, Oracle, MySQL, and Microsoft SQL
Server.
• Information is presented to the user as relations, i.e. as
a collection of tables with each table consisting of a set
of rows and columns.
• Object Databases:
• Came into existence in 1980s, to overcome technical difficulties
encountered with RDBMS.
• Information is represented in the form of objects as used
in object-oriented programming.
• Popular OOBDMS: Jade, ObjectDB (works with Java)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Types of Databases…(2)
• NoSQL Databases:
• Object Databases which came into existence in late 2000s.
• Further divided in
• Key-value Stores,
• Document Oriented Databases,
• Wide Column Stores &
• Graph Databases.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
NoSQL Databases…(1)
• Key value stores:
• Designed for storing, retrieving & managing dictionaries or hash
tables. Redis
• Dictionaries contain collections of objects or records, which contain
many different fields or data.
• These records are stored and retrieved using a key that uniquely Memcached
identifies the record, and is used to find the data within the database.
• Popular examples: Redis, Memcached, Amazon DynamoDB
• Use Cases: Caching, session management, user preferences.
Amazon
DynamoDB
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
NoSQL Databases…(2)
• Document Oriented Databases:
• Also known as document store.
• Designed for storing, retrieving & managing document oriented
information.
• The central concept of document oriented databases are the
documents containing information in one of these encodings:
• XML, YAML, JSON & BSON
• Use Cases: Content management systems, e-commerce
applications, real-time analytics.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
NoSQL Databases…(3)
• Graph Databases:
• Uses graph structures for queries with nodes & edges.
• Relates the data items in the store to a collection of nodes and edges.
• The relationships allows data to be linked directly & retrieved faster.
• Querying relationships is fast because they are perpetually stored in
the database.
• Useful for heavily inter-connected data
• Popular Examples: Neo4j, Azure Cosmos DB, SAP HANA
• Use Cases: Social networks, fraud detection, network topology.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
You might ask…
WHY TO STUDY
NoSQL?
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Why NoSQL Database..?
• The rise of Object Oriented Programming Languages.
• Which in turn, made possible to store, retrieve & modify data as objects.
• Helps programmers to maintain the consistency between the database & the
programming language.
• With NoSQL data can be stored in form of JSON, XML, YAML and even graphs, which
are frequently used to write application logic.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Why NoSQL Database..?
• The data can be stored as collection of documents with structure that can vary from being
very flexible to highly rigid.
• Are very fast, since these fixed table schemas, and they do not use Joins.
• These are designed to scale horizontally, instead of vertical, in case of RDBMS, thus occupying
less space.
• High speeds. Data stored with NoSQL databases has access to RAM & SSD, which are faster
than the HDD where the RDBMS store their data.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Example…
• Imagine a scenario, where we have 2 tables, person & friends.
• Person has all the personal information about people, and friends contains list of their friends.
• To find all of Jack’s friends, the SQL query would be:
• The same query with Graph Database, a type of NoSQL could be:
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
You might ask…
HOW ARE THEY
DIFFERENT FROM
SQL?
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Key Differences…(1)
FACTORS SQL NoSQL
Supports various data models,
Structured schemas with including document, key-
Data Model
tables, rows & columns. value, wide-column, and graph
formats.
Scaled vertically by adding
Scaled horizontally across
Scalability more resources to a single
multiple servers.
server.
Rigid schemas requiring Flexible schemas allowing
Flexibility
predefined structures. dynamic data structures.
Strong ACID (Atomicity, Varying levels of consistency
Transactions Consistency, Isolation, and may prioritize availability
Durability) compliance. and partition tolerance
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Key Differences…(2)
FACTORS SQL NoSQL
Various APIs and query
Structured Query Language
languages tailored to the
Query Language (SQL) for defining and
specific NoSQL type (e.g.,
manipulating data.
MongoDB Query Language).
Ideal for applications with
Suitable for applications large-scale data, real-time
requiring complex transactions analytics, content
Use Cases
and data integrity, such as management, and flexible
banking systems. data models, such as social
media platforms.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
You might ask…
Where does MongoDB
fit in?
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
MongoDB…
• MongoDB is a document-oriented NoSQL database that stores data in JSON-like BSON
documents, allowing for flexible and hierarchical data structures without the need for
predefined schemas.
• The primary data unit is the document, which is a BSON (Binary JSON) object.
• Documents are grouped into collections, which are analogous to tables in SQL databases.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
SQL v/s MongoDB…(1)
• We will store the same information in both SQL & MongoDB.
• Let’s say we want to store info about a user named Leslie.
• In SQL, we would create a table ‘user’ with column for each piece of information, with id as a
unique identifies to identify each user.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
SQL v/s MongoDB…(2)
• In Mongo, we would create a document with key-value pairs for each piece of information.
• With _id as the unique identifier
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
SQL v/s MongoDB…(3)
• Let’s add her current location to the DB in form of latitude & longitude.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
SQL v/s MongoDB…(4)
• Let’s also add her hobbies. Here comes the problem with RDBMS.
• Since single user could have multiple hobbies, in SQL we need to create a separate table.
• And then join the two tables to get data for Leslie.
• Since Mongo DB can store arrays, there is no need to have a separate document. Hence no
need for join.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
SQL v/s MongoDB…(5)
• This time we want to add Leslie’s job history to the DB.
• For that we will create another table in SQL DB
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping:
Drawing comparisons in the
names or terms.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(1)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(2)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(3)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(4)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(5)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(6)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Term Mapping…(7)
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
MongoDB
Concepts:
• Replication
• Sharding
• Indexes
• Aggregation
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Replication…(1)
• Replication in MongoDB is the process of making multiple copies of data
across multiple MongoDB servers.
• The goal of replication is to improve data availability, fault tolerance, and
scalability.
• Replication is achieved using a replica set, which is a group of MongoDB
instances that share the same data set.
• A replica set contains at least two MongoDB instances, including a primary
node and secondary nodes.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Replication…(2)
• Primary Node:
• Handles all write operations.
• Replicates data changes to secondary nodes.
• If the primary fails, an election process selects a new primary from
the secondaries.
• Secondary Node:
• Maintain copies of the primary's data.
• Can handle read operations if configured
• Continuously replicate data from the primary.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Sharding…
• Sharding in MongoDB is a horizontal scaling technique that distributes data
across multiple machines or clusters, known as shards.
• Each shard holds a subset of the total data, allowing the database to handle
large volumes of data and high-throughput operations by parallelizing
queries and write operations.
• Sharding is used to:
• Scale Out: Accommodate growing data sizes beyond the capacity of a
single server.
• Improve Performance: Distribute the load, reducing bottlenecks and
enhancing read/write speeds.
• Ensure Availability: Distribute data across different servers or data
centers to enhance fault tolerance.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Indexes…
• Indexes in MongoDB are data structures that store a portion of the collection’s data in an
easy-to-traverse form, to make queries more efficient.
• They work similarly to indexes in books, allowing MongoDB to quickly locate and access the
required documents without scanning the entire collection.
• Types of Indexes:
• Single Field
• Compound
• Multikey
• Text
• Geospatial
• Hashed
• Unique
• Sparse
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
Aggregation…
• In MongoDB, aggregation is a way to process large numbers of documents in a
collection by passing them through a series of stages, or pipeline.
• The aggregation pipeline can perform a variety of operations,
including:
• Filtering: Narrowing down the list of documents based on a set of
criteria.
• Sorting: Reordering documents based on a chosen field.
• Grouping: Processing multiple documents together to form a
summarized result.
• Transforming: Changing the structure of documents, such as
removing or renaming fields.
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years
You might be wondering…
When would
we install it?
The answer is
RIGHT NOW…..
Rohit Jain || Full Stack Trainer (Responsive Design, MERN, PHP) || Exp: 10 years