Module 3
Module 3
MongoDB ─ A Database for the Modern Web: What is MongoDB?, JSON, JSON Structure, MongoDB
Structure, Document Store Example, MongoDB as a Document Database, Transaction Management in
MongoDB, Scaling Up vs. Scaling Out, Features of MongoDB, Secondary Indexes, Replication,
Memory Management, Auto Sharding, Aggregation and MapReduce, Collection and Database, Schema
Design and Modeling, Reference Data Model, Embedded Data Model, Data Types, Installing
MongoDB on Linux, Windows, Starting MongoDB On Windows, Use Cases: Explore MongoDB
database
Reference : https://learn.mongodb.com/courses/start-here-introduction-to-mongodb
OVERVIEW OF NON-RELATIONAL
DATABASES
WHERE IT BEGAN: RELATIONAL
KEY FEATURES OF RELATIONAL
DATABASES
Strength
Key/Value Database
Key Value
Age 40
Strength
Graph Database
● Traverses the connections
between data rapidly
Graph: Example
221B
Baker :addr
ess
St.
: li
ke
Sherlock
d
ress
s Holmes
Post
i e nd :fr
r
:f h wi iend
1
:add
:p t
os wi th s
te
d
os
te
:p
os
te
os
d
d
:like
d
:p
te
Post Post
2 Post
3 1
Structure
Strengths
Strengths
● No complex mapping
Document Model Example
{
"_id": {
ObjectId("5ef2d4b45b7f11b6d7a"), "_id":
"user_id": "Sherlock Holmes", ObjectId("6ef8d4b32c9f12b6d4a"),
"age": 40, "user_id": "John Watson",
"address": "age": 45,
{ "address":
“Country: “England” {
“City”: “London”, “Country: “England”
“Street”: “221B Baker St.” “City”: “London”,
}, “Street”: “221B Baker St.”
“Hobbies”:[ violin, crime-solving ] },
} “Medical license”: “Active”
}
THE DOCUMENT MODEL
FOR GENERAL PURPOSE USE, THE
DOCUMENT MODEL PREVAILS AS
THE PREFERRED MODEL BY
DEVELOPERS AND DATABASE
ADMINISTRATORS.
WHAT IS MONGODB ?
MONGODB
"Status": "A"
}
TRANSACTIONS IN
MONGODB
https://www.mongodb.com/docs/manual/core/transactions/
WHAT IS A TRANSACTION?
A single unit of logic composed of multiple different database
operations, which exhibits the following properties:
patients collection
{
date of visit "_id": 2395652,
"name": "AJ",
"current_weight": 70,
https://www.mongodb.com/resources/basics/horizontal-vs-vertical-scaling
FEATURES OF MONGODB
https://www.mongodb.com/resources/products/capabilities/features
Document Model
● MongoDB has been designed with developer
productivity and flexibility in mind.
● It is a document-oriented database, which
means that data is stored as documents, and
documents are grouped in collections.
● The document model is a lot more natural for
developers to work with because documents
are self-contained and can be treated as
objects.
● This means that developers can focus on the
data they need to store and process, rather
than worrying about how to split the data
across different rigid tables.
● Documents are grouped together
in collections. However, the documents in a
single collection don't necessarily need to
have exactly the same set of fields. This is
what we call a “flexible schema.”
DOCUMENT MODEL
● When your data only resides in a single server, it is exposed to multiple potential
points of failure, such as a server crash, service interruptions, or even good old
hardware failure. Any of these events would make accessing your data nearly
impossible.
● Replication allows you to sidestep these vulnerabilities by deploying multiple
servers for disaster recovery and backup.
● Horizontal scaling across multiple servers greatly increases data availability,
reliability, and fault tolerance.
● Potentially, replication can help spread the read load to the secondary members
of the replica set with the use of read preference.
REPLICATION IN MONGODB
● The number one issue that many technical support teams fail to address with
their users is indexing.
● Indexes are intended to improve search speed and performance.
● A failure to properly define appropriate indexes can and usually will lead to
accessibility issues, such as problems with query execution and load balancing.
● Without the right indexes, a database is forced to scan documents one by one to
identify the ones that match the query statement.
● But if an appropriate index exists for each query, user requests can be optimally
executed by the server.
INDEXING
● MongoDB offers a broad range of indexes and features with language-specific sort
orders that support complex access patterns to datasets.
● Notably, MongoDB indexes can be created on demand to accommodate real-time,
ever-changing query patterns and application requirements.
● They can also be declared on any field within any of your documents, including
those nested within arrays.
DATABASE TRIGGERS
● Database triggers in MongoDB Atlas are a powerful feature that allow you to
execute code when certain events occur in your database.
● For example, you can use triggers to execute a script when a document is
inserted, updated, or deleted. Triggers can also be scheduled to execute at
specific times.
● MongoDB Atlas allows you to create and manage triggers in a simple, intuitive
way.
● Database triggers are a great way to perform audits, ensure data consistency and
data integrity, and to perform complex event processing.
TIME SERIES DATA
● Time series data is most commonly generated by a device, such as a sensor, that
records data over time. The data is stored in a collection of documents, each of
which contains a timestamp and a value.
● MongoDB provides a number of features to help you manage time series data.
● The native time series collections in MongoDB are designed to be storage-
efficient and perform well with sequences of measurements.
● You have a number of parameters to control the storage of time series data,
including the granularity (the time span between measurements) and the
expiration threshold of old data.
AD-HOC QUERIES
● At the end of the day, optimal load balancing remains one of the holy grails of large-
scale database management for growing enterprise applications.
● Properly distributing millions of client requests to hundreds or thousands of servers
can lead to a noticeable (and much appreciated) difference in performance.
● Fortunately, via horizontal scaling features like replication and sharding, MongoDB
supports large-scale load balancing.
● The platform can handle multiple concurrent read and write requests for the same
data with best-in-class concurrency control and locking protocols that ensure data
consistency.
● There’s no need to add an external load balancer—MongoDB ensures that each and
every user has a consistent view and quality experience with the data they need to
access.
COLLECTIONS IN THE DOCUMENT
MODEL
Document Collection
JSON SCHEMA
https://www.mongodb.com/docs/manual/data-modeling/
DATA MODELING
• Data modeling refers to the organization of data within a database and the links between
related entities.
• Data in MongoDB has a flexible schema model, which means:
• Documents within a single collection are not required to have the same set of fields.
• A field's data type can differ between documents within a collection.
• The flexible data model lets you organize your data to match your application's needs.
• Use case : Your company tracks which department each employee works in. You can embed
department information inside of the employee collection to return relevant information in a single
query
You must determine a table's schema Your schema can change over time as the
before you insert data. needs of your application change.
You often need to join data from several The flexible data model lets you store data
different tables to return the data needed to match the way your application returns
by your application. data, and avoid joins. Avoiding joins across
multiple collections improves performance
and reduces your deployment's workload.
EMBEDDED DATA MODELS
• Embedding would result in duplication of data but would not provide sufficient read performance advantages
to outweigh the implications of the duplication.
• For example, when the embedded data frequently changes.
• You need to represent complex many-to-many relationships or large hierarchical data sets.
• The related entity is frequently queried on its own. For example, if you have employee and department data,
you may consider embedding department information in the employee documents. However, if you often
query for a list of departments, your application will perform best with a separate department collection that
is linked to the employee collection with a reference.
SCHEMA DESIGN
• The schema design process helps you prepare an effective schema for
your application.
• The schema design process consists of the following steps:
• Identify your workload - Identify the operations that your application
runs most frequently.
• Map relationships - Identify the relationships in your application's data
and decide whether to link or embed related data.
• Apply design patterns - Apply schema design patterns to optimize reads
and writes.
REFERENCE
https://www.mongodb.com/docs/manual/