0% found this document useful (0 votes)
3 views30 pages

Mongo DB (1)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

Mongo DB

Made by - Kulanshu Sharma


• What is MongoDB?
• Why Use MongoDB?
• Brief Overview of JSON.
• Design Goals For MongoDB Server And Database.
Index •

MongoDB Tools
Usage of Various MongoDB Tools Available.
• MongoDB Development Architecture.
• MongoDB CRUD Operations.
• MongoDB DataTypes.
• MongoDB Data Models.
• Challenges for Data Modelling in MongoDB.
• CAP Theorem.
• Analogy Between RDBMS & MongoDB
• References
What is Mongo DB?

• MongoDB is a document database built on a horizontal scale-out


architecture that uses a flexible schema for storing data.
• Founded in 2007, MongoDB has a worldwide following in the developer
community.
• Instead of storing data in tables of rows or columns like SQL databases,
each record in a MongoDB database is a document described in BSON, a
binary representation of the data. Applications can then retrieve this
information in a JSON format.
Why Use Mongo DB?
• As a document database, MongoDB makes it easy for developers to store
structured or unstructured data.
• MongoDB can also handle high volume and can scale both vertically or
horizontally to accommodate large data loads.
• MongoDB is available in any major public cloud (such as AWS, Azure,
and Google Cloud) through MongoDB Atlas, in large data centers through
the Enterprise Advanced edition, or free through the source-available
Community edition.
Brief Overview of JSON.
• JavaScript Object Notation (JSON) is a lightweight data-interchange format. It is
easy for humans to read and write. It is easy for machines to parse and generate.
• JSON is built on two structures:
1. A collection of name/value pairs. In various languages, this is realized as
an object, record, struct, dictionary, hash table, keyed list, or associative array.
2. An ordered list of values. In most languages, this is realized as an array, vector,
list, or sequence.
• Syntax→ '{"name":“xyz", "age":10, "car":null}’
• JSON files are saved with .json extension.
DESIGN GOALS FOR MONGODB SERVER
AND DATABASE
• Scalability: MongoDB should be able to handle growing amounts of data and
increasing numbers of users. This involves both vertical scaling (adding more
resources to a single server) and horizontal scaling (distributing data across
multiple servers).
• Performance: MongoDB should provide fast read and write operations, low
latency, and efficient query execution. This often involves optimizing indexing,
data modeling, and server configuration.
• Flexibility: MongoDB should accommodate changing requirements and
evolving data structures. It should support dynamic schemas, allowing for easy
addition and modification of fields within documents.
• Reliability: MongoDB should ensure data integrity, durability, and availability. This
includes features like replication for fault tolerance, automatic failover, and data
consistency mechanisms.
• Security: MongoDB should provide robust security features to protect data from
unauthorized access, tampering, and other threats. This includes authentication,
authorization, encryption, and auditing capabilities.
• Manageability: MongoDB should be easy to deploy, configure, monitor, and maintain.
It should provide tools for performance optimization, troubleshooting, backup, and
recovery.
• Interoperability: MongoDB should integrate well with other systems and tools in the
technology stack. This includes support for various programming languages,
frameworks, and data interchange formats.
• Cost-effectiveness: MongoDB should deliver value for the investment made in terms
of hardware, software, and operational resources. This involves optimizing resource
utilization, minimizing licensing costs, and maximizing productivity.
MongoDB Tools
The MongoDB Database Tools are a collection of command-line utilities for
working with a MongoDB deployment.
• MongoDB shell
• MongoDB compass (GUI)
• Atlas CLI
• Studio
• NOSQL booster
Usage of Various MongoDB Tools Available

• MongoDB shell-: The MongoDB Shell, mongosh, is a JavaScript and


Node.js REPL environment for interacting with MongoDB deployments in
Atlas, locally, or on another remote host. You can use the MongoDB Shell
to test queries and interact with the data in your MongoDB database.
• MongoDB compass (GUI)-:You can easily explore and manipulate your
database with Compass, the GUI for MongoDB. Intuitive and flexible,
Compass provides detailed schema visualizations, real-time performance
metrics, sophisticated querying abilities.
• Atlas CLI-:The Atlas CLI (mongodb-atlas) is a unified command line interface
for managing MongoDB Atlas throughout the entire software development
lifecycle, from your local environment all the way to the cloud. Use short,
intuitive commands in your terminal to accomplish complex database
management tasks in seconds.

• Studio-: A visual tool to view, edit, and design Realm Database files.

• NOSQL booster-: The Smartest IDE for MongoDB NoSQLBooster is a cross-


platform GUI tool for MongoDB Server 3.6-7.0, which provides a build-in
MongoDB script debugger, comprehensive server monitoring tools, chaining
fluent query, SQL query, query code generator, task scheduling, ES2020
support, and advanced IntelliSense experience.
MongoDB Development Architecture
The architecture of MongoDB development typically revolves around
several key components:
• MongoDB Database: At the core is the MongoDB database itself.
MongoDB is a NoSQL database that stores data in flexible, JSON-like
documents, meaning fields can vary from document to document and data
structure can be altered over time.
• Collections: MongoDB organizes data into collections, which are groups
of documents. Collections are roughly equivalent to tables in relational
databases.
• Documents: Documents are individual records within a collection. They are
composed of field-value pairs and are the basic unit of data in MongoDB.
Documents in a collection can have different fields and structures.
• Indexes: MongoDB supports the creation of indexes to improve the query
performance. Indexes can be created on any field or subfield in a document and
can greatly enhance the speed of data retrieval.
• Replica Sets: MongoDB replica sets provide high availability and data
redundancy. A replica set is a group of MongoDB servers that maintain the same
data set, providing redundancy and fault tolerance. In a replica set, one node is
the primary node that receives all write operations, while the other nodes
(secondaries) replicate data from the primary.
• Security: MongoDB provides various security features such as authentication,
authorization, encryption, auditing, and network isolation to protect data and
ensure compliance with security standards.
• Sharding: Sharding is the process of splitting data across multiple MongoDB
instances. It's used for horizontal scaling to ensure that data sets grow within
acceptable performance bounds. Sharding involves distributing data across
multiple servers based on a shard key.
• MongoDB Drivers: MongoDB provides official and community-supported
drivers for various programming languages. These drivers allow developers to
interact with MongoDB databases from their application code.
• Application Layer: This is where your application logic resides. It interacts
with MongoDB using the MongoDB drivers, executing CRUD (Create, Read,
Update, Delete) operations, querying data, and handling the results.
• Middleware and Frameworks: Often, developers use middleware and
frameworks to streamline the development process. These can include
Express.js for Node.js applications, Spring Data MongoDB for Java
applications, or frameworks in other languages.
MongoDB CRUD Operations
• C – Create
*Create or insert operations add new documents to a collection. If the collection does
not currently exist, insert operations will create the collection.
* MongoDB provides the following methods to insert documents into a collection:
1) db.collection.insertOne() 2)db.collection.insertMany()
* In MongoDB, insert operations target a single collection. All write operations in
MongoDB are atomic on the level of a single document.
• R – Read
*Read operations retrieve documents from a collection; i.e. query a
collection for documents.
*MongoDB provides the following method to read documents from a
collection:
1) db.collection.find()
*You can specify query filters or criteria that identify the documents to
return.
• U – Update
*Update operations modify existing documents in a collection.
*MongoDB provides the following methods to update documents of a
collection:
1) db.collection.updateOne() 2) db.collection.updateMany()
3) db.collection.replaceOne()
*In MongoDB, update operations target a single collection. All write
operations in MongoDB are atomic on the level of a single document.
*You can specify criteria, or filters, that identify the documents to update.
These filters use the same syntax as read operations.
• D – Delete
*Delete operations remove documents from a collection.
*MongoDB provides the following methods to delete documents of a
collection:
1) db.collection.deleteOne() 2)db.collection.deleteMany()
*In MongoDB, delete operations target a single collection. All write
operations in MongoDB are atomic on the level of a single document.
*You can specify criteria, or filters, that identify the documents to
remove. These filters use the same syntax as read operations.
MongoDB DataTypes
MongoDB Server stores data using the BSON format which supports some
additional data types that are not available using the JSON format.
• String
• ObjectID
• Date
• Int32
• Long
• Decimal128
• and all the basic ones.
MongoDB Data Models
• For modeling data in MongoDB, two strategies are available. These
strategies are different and it is recommended to analyze scenario for a
better flow.
• The two methods for data model design in MongoDB are:
1. Embedded Data Model
2. Normalized Data Model
Embedded Data Model:
• This method, also known as the de-normalized data model, allows you
embed all of the related documents in a single document.
• These nested documents are also called sub-documents.
• Example:
Normalized Data Model(References)
• In a normalized data model, object references are used to express the
relationships between documents and data objects. Because this approach
reduces data duplication, it is relatively simple to document many-to-many
relationships without having to repeat content.
• Normalized data models are the most effective technique to model large
hierarchical data with cross-collection relationships.
• Example:
Challenges for Data Modelling in MongoDB

• Schema Design: MongoDB's flexible schema allows for dynamic data structures
within documents. While this flexibility is beneficial for accommodating
evolving data requirements, it can also lead to challenges in maintaining
consistency and enforcing data integrity, especially in complex data models.
• Data Growth and Sharding: MongoDB's scalability is achieved through
horizontal sharding, which distributes data across multiple shards. However,
designing an effective sharding strategy requires careful consideration of factors
such as shard key selection, data distribution, and query isolation. Managing data
growth and maintaining balanced shard distribution can be challenging,
especially in large-scale deployments.
CAP Theorem

The CAP theorem, also known as Brewer's theorem, states that in


a distributed computer system, it is impossible to simultaneously
guarantee all three C (Consistency), A (Availability), P (Partition
tolerance).
Partition tolerance (P) is very important so , a distributed database
must choose between maintaining consistency (C) or availability
(A).
C - Consistency-:

• Consistency in the context of the CAP theorem refers to the property that
all nodes in a distributed system return the same data when queried at the
same time. In other words, consistency ensures that all clients see the
same view of the data at any given moment, regardless of which node
they query.

• Consistency is often associated with support for transactions, which allow


multiple operations to be grouped together into an atomic, isolated, and
consistent unit of work. Transactions ensure that either all operations
within the transaction are successfully applied, or none of them are
applied, maintaining the consistency of the data.
A - Availability-:
• Availability in the context of the CAP theorem refers to the property that
every request made to a distributed system receives a response, without
any guarantee that the response contains the most recent write. In other
words, an available system remains operational and responsive, even in
the face of network partitions or node failures. While availability is
critical for ensuring uninterrupted service, it may come at the cost of
sacrificing strong consistency in certain scenarios.
• Availability is often improved through load balancing, where incoming
requests are distributed across multiple nodes in the system to prevent any
single node from becoming overwhelmed with traffic. Load balancers
help ensure that the system remains responsive and available to handle
incoming requests.
P – Partition Tolerance-:
• Partition Tolerance in the context of the CAP theorem refers to the ability
of a distributed system to continue operating and serving requests despite
network partitions, which may cause communication breakdowns between
nodes. In other words, partition tolerance ensures that the system can
tolerate the failure of communication between nodes and still provide
some level of service.
• Partition-tolerant systems are designed to isolate and contain the effects of
network partitions, preventing them from spreading across the entire
system and causing cascading failures. This may involve implementing
fault isolation boundaries or employing techniques such as partitioning
and replication to limit the impact of partitions. It is a critical property for
building robust and fault-tolerant distributed systems.
Analogy Between RDBMS & MongoDB
RDBMS MongoDB
Structured Storage: RDBMS stores data in Flexible Schema: MongoDB stores data as flexible
structured tables with predefined schemas, much JSON-like documents, allowing for dynamic
like neatly organized rows and columns in a schemas where each document can have its own
spreadsheet. structure.
Strict Relationships: It enforces relationships Scalability: It offers horizontal scalability through
between different data entities using foreign keys, sharding, distributing data across multiple nodes,
ensuring data integrity and consistency. enabling high-performance and large-scale
deployments.
SQL Queries: RDBMS uses SQL (Structured
Query Language) for querying and manipulating NoSQL Queries: MongoDB uses a flexible query
data, providing a standardized way to interact with language, allowing for complex queries,
the database. aggregations, and document-based operations,
catering to diverse application needs without strict
adherence to a rigid schema.
CP : RDBMS supports CP of CAP AP: MongoDB supports AP of CAP
theorem that prioritize consistency and theorem that prioritize availability and
partition tolerance sacrifice availability. partition tolerance sacrifice strong
In this system, even in the presence of a consistency. This system prioritize
network partition, data remains availability over consistency, so they
consistent across all nodes, but some may return stale or divergent data in
nodes might become unreachable or the event of a network partition.
return errors until the partition is However, they continue to function and
resolved. serve requests, ensuring high
SQL databases needs to perform availability.
transaction so they prefer Consistency NoSQL provides the data instantly so
over Availability they compromise the Consistency and
choose Availability.
References
• www.mongodb.com
• www.json.org
• https://nosqlbooster.com/
• www.geeksforgeeks.org
• www.w3schools.com
Thank you!!

You might also like