SlideShare a Scribd company logo
1 of 25
Download to read offline
MongoDB and
Best Practices
BY - ASHISH RATHORE
Unleash the Power of NoSQL with
MongoDB - Scalable, Secure, and Agile
Agenda
KEY TOPICS DISCUSSED IN
THIS PRESENTATION
Introduction to MongoDB
MongoDB Data Modeling
Working with MongoDB
MongoDB security
Scaling MongoDB
Real-world use cases of MongoDB
Best practices for using MongoDB
What is MongoDB?
MongoDB is a NoSQL database management system
that allows for flexible and scalable data storage
and retrieval.
Unlike traditional relational databases, MongoDB
uses a document model to store data, allowing for
easy and fast data access.
MongoDB is an open-source project that is
continuously developed and maintained by
MongoDB, Inc.
MongoDB offers powerful indexing capabilities,
allowing for efficient querying of data.
Relational databases are based on the traditional
tabular model.
Relational databases require a schema to be
defined upfront
Relational databases are vertically scalable,
meaning that they are scaled up by adding more
resources to a single server, which can be
expensive and limited.
Relational databases may be better suited for
complex queries that require joining multiple
tables.
Relational databases are often used for
transactional systems that require strict data
consistency and integrity, such as banking or e-
commerce applications.
NoSQL databases use a variety of data models,
such as document-based, key-value or graph db.
NoSQL databases offer more flexible schema
design.
NoSQL databases, are horizontally scalable,
meaning that they can be scaled out by adding
more servers to the cluster, which allows for easier
and more cost-effective scaling.
NoSQL databases generally offer better
performance and faster data access especially
when dealing with large amounts of data.
NoSQL databases are often used for applications
that require flexible schema design, scalability, and
fast data access, such as social media, gaming, or
IoT applications.
NOSQL(not only SQL )
SQL (Relational)
RDBMS MONGODB
Database Instance MongoDB Instance
Schema Database
Table Collection
Row Document
rowid _id
Belongs to a Collection
Has a field "_id" (unique within the
document)
Belongs to a Database
Each JSON Document:
Each Collection:
Terminology
1 2
MongoDB Data Modeling: Designing for
Flexibility and Performance
SCHEMA
DESIGN
MongoDB offers
flexible schema
design, allowing
for changes to
the schema as
the application
evolves.
DOCUMENT
MODEL
MongoDB uses a
document model
to store data,
where each
document
represents a
single object or
entity.
DATA
STRUCTURE
MongoDB
supports nested
and hierarchical
data structures,
allowing for more
complex data
models than
traditional
relational
databases.
INDEXING
MongoDB provides
powerful indexing
capabilities to
support fast data
retrieval.
BEST
PRACTICES
Denormalizing data,
Avoiding overly
complex,
Using indexing
strategically to
support common
queries.
3 4 5
Working with
MongoDB
To insert a new document into a collection, you can
use the insertOne() method:
To update an existing document, you can use the
updateOne() method:
To delete a document, you can use the deleteOne()
method:
db.collection('users').insertOne({ name: 'John', age: 30, email: 'john@example.com' });
CRUD
Operations
CRUD OPERATIONS IN
MONGODB ARE SIMILAR
TO THOSE IN OTHER
DATABASES, BUT USE
DIFFERENT SYNTAX. db.collection('users').updateOne({ name: 'John' }, { $set: { age: 31 } });
db.collection('users').deleteOne({ name: 'John' })
To find all documents in a collection with a specific
field value, you can use the find() method with a query
object
To find documents that match multiple criteria, you
can use the $and and $or operators:
db.collection('users').find({ age: 30 });
Querying
MongoDB Data
MONGODB PROVIDES A
RICH QUERY LANGUAGE
THAT ALLOWS YOU TO
SEARCH FOR DOCUMENTS
BASED ON SPECIFIC
CRITERIA db.collection('users').find({ $or: [{ age: 30 }, { age: 31 }] });
To calculate the total number of users in a collection,
you can use the $count operator:
To group documents by a specific field and calculate
aggregate values for each group, you can use the
$group operator:
db.collection('users').aggregate([{ $count: 'total_users' }]);
Aggregation
and Reporting
MONGODB'S
AGGREGATION
FRAMEWORK PROVIDES A
POWERFUL WAY TO
ANALYZE DATA WITHIN
COLLECTIONS
db.collection('users').aggregate([{ $group: { _id: '$age', total: { $sum: 1 } } }]);
MongoDB
Security:
Authentication,
Authorization,
Best Practices,
and Pitfalls
MongoDB provides several mechanisms for
authentication and authorization, including built-in
and external authentication methods.
Built-in authentication methods include
username/password authentication, LDAP
integration, and X.509 certificate authentication.
Authorization in MongoDB is managed through
roles, which control user access to specific
databases, collections, and operations.
Authentication
and
Authorization
in MongoDB
> use admin
> db.createUser(
{
user: "justAUser", pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "readWrite", db: "demo" },
{ role: "read", db: "finances" } ]
}
)
Enabling authentication and authorization for all
users and applications accessing the database.
Using strong passwords and rotating them
regularly.
Restricting network access to MongoDB servers
and using encryption for data in transit.
Implementing access controls and least privilege
principles to limit user access to only what they
need to do their job.
Regularly applying patches and updates to the
MongoDB software and underlying operating
system.
Best Practices
for Securing
MongoDB
Scaling
MongoDB:
Options,
Strategies, and
Best Practices
Vertical scaling Horizontal scaling
Adding more resources, such as
CPU, memory, or storage, to a
single server.
Adding more servers to a cluster,
distributing data across them to
increase performance and capacity.
Scaling Options for
MongoDB
MONGODB PROVIDES SEVERAL OPTIONS FOR SCALING,
INCLUDING:
Sharding in MongoDB
Sharding is a horizontal scaling strategy that involves dividing a
large MongoDB dataset into smaller partitions called shards, which
are distributed across multiple servers.
MongoDB uses a sharded cluster to manage shard distribution and
data movement, and provides several sharding strategies, including
range-based, hash-based, and zone sharding.
Sharding requires careful planning and configuration to ensure
optimal performance, consistency, and availability.
MongoDB also provides tools such as the balancer and shard status
command to manage and monitor the sharded cluster.
Real-World Use
Cases of
MongoDB
These percentages represent the
market share of MongoDB in each use
case, indicating its popularity and
adoption in those industries.
E-commerce Applications
40%
Social Media Applications
25%
IoT and Big Data Applications
20%
Mobile Applications
15%
Best Practices
for Using
MongoDB
Hardware
and
Application
Patterns
Ensure your working set fits in RAM:
As with most databases, MongoDB performs best when the working
set (indexes and most frequently accessed data) fits in RAM. RAM
size is the most important factor for hardware; other optimizations
may not significantly improve the performance of the system if
there is insufficient RAM. Use the db.serverStatus() command to
view an estimate of the the current working set size.
Test every query in your application with explain():
The number of documents returned
The number of documents read
Which indexes were used
The number of index entries scanned
How long the query took to resolve in milliseconds (when using
the executionStats mode)
MongoDB provides an explain plan capability that shows
information about how a query will be, or was, resolved, including:
Check the uptime
The uptime field in the output of db.serverStatus() tells you how long the server has been running. If the server has been
running for a long time without any issues, this is a good sign that things are running smoothly.
Look at the connections
The connections field in the output of db.serverStatus() tells you how many client connections are currently active. If the
number of connections is consistently high, it may indicate that the server is under heavy load and may need more
resources.
Monitor the opcounters
The opcounters field in the output of db.serverStatus() provides information about the number of operations performed
on the server, including inserts, updates, deletes, and queries. If the opcounters are consistently high, it may indicate that
the server is under heavy load and may need more resources.
Check the network activity
The network field in the output of db.serverStatus() provides information about the network activity of the server. If the
network activity is consistently high, it may indicate that the server is under heavy load and may need more resources.
Schema
Design
Store all data for a record in a single document:
MongoDB provides ACID compliance at the document level. When
data for a record is stored in a single document the entire record
can be retrieved in a single seek operation, which is very efficient.
In some cases it may not be practical to store all data in a single
document, or it may negatively impact other operations.
Avoid large documents
The maximum size for documents in MongoDB is 16 MB. In
practice, most documents are a few kilobytes or less. Rather than
maintaining lists of records in a single document, instead make
each record a document.
Best
Practices for
Data
Indexing,
Security,
and
Scalability
Boosting MongoDB Query Performance with Indexing
Use indexing to improve query performance, but be mindful of the
impact on write performance and index size. Proper use of indexes
can greatly improve the performance of your MongoDB application.
Securing Your MongoDB Deployment
Implement proper authentication and authorization mechanisms
to secure your MongoDB deployment, and follow other security
best practices such as encrypting data at rest and in transit.
Scaling MongoDB for Success
Plan for scalability from the beginning, and consider horizontal
scaling options such as sharding to handle increasing data
volumes and user traffic.
Backup and
Recovery
MongoDB Backup with mongodump
Backing up MongoDB is a critical task to ensure data is safe and
recoverable. MongoDB provides a command-line tool called
"mongodump" to take a backup of data in BSON format. To create a
backup using "mongodump", open the command prompt and run
the following command:
MongoDB Recovery with mongorestore
To restore the backup created using "mongodump", MongoDB
provides a command-line tool called "mongorestore". To restore a
backup using "mongorestore", open the command prompt and run
the following command:
mongodump --host <hostname> --port <port> --db <database-name> --out <backup-directory>
mongorestore --host <hostname> --port <port> --db <database-name> <backup-directory>
What's Next in NOSQL
As the field of data management continues to evolve, NoSQL
databases are likely to play an increasingly important role in
helping organizations to manage their growing volumes of data.
Some of the key areas where NoSQL databases are likely to
continue to innovate and expand in the coming years include:
Advanced analytics and machine learning capabilities
Greater support for distributed architectures and
microservices
Integration with emerging technologies such as blockchain
and IoT
Overall, the future looks bright for NoSQL databases, as
organizations continue to seek out scalable, flexible, and cost-
effective solutions for managing their data in the years to come.
Do you have
any questions?

More Related Content

Similar to Introduction to MongoDB and its best practices

Hands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYC
Hands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYCHands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYC
Hands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYCLaura Ventura
 
MongoDB.local Sydney: An Introduction to Document Databases with MongoDB
MongoDB.local Sydney: An Introduction to Document Databases with MongoDBMongoDB.local Sydney: An Introduction to Document Databases with MongoDB
MongoDB.local Sydney: An Introduction to Document Databases with MongoDBMongoDB
 
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...ijcsity
 
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...ijcsity
 
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...ijcsity
 
MongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data scienceMongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data sciencebitragowthamkumar1
 
A Study on Mongodb Database.pdf
A Study on Mongodb Database.pdfA Study on Mongodb Database.pdf
A Study on Mongodb Database.pdfJessica Navarro
 
A Study on Mongodb Database
A Study on Mongodb DatabaseA Study on Mongodb Database
A Study on Mongodb DatabaseIJSRD
 
nosql [Autosaved].pptx
nosql [Autosaved].pptxnosql [Autosaved].pptx
nosql [Autosaved].pptxIndrani Sen
 
SQL vs NoSQL, an experiment with MongoDB
SQL vs NoSQL, an experiment with MongoDBSQL vs NoSQL, an experiment with MongoDB
SQL vs NoSQL, an experiment with MongoDBMarco Segato
 
Performance analysis of MongoDB and HBase
Performance analysis of MongoDB and HBasePerformance analysis of MongoDB and HBase
Performance analysis of MongoDB and HBaseSindhujanDhayalan
 
Klevis Mino: MongoDB
Klevis Mino: MongoDBKlevis Mino: MongoDB
Klevis Mino: MongoDBCarlo Vaccari
 
MongoDB presentation
MongoDB presentationMongoDB presentation
MongoDB presentationHyphen Call
 
MongoDB_Spark
MongoDB_SparkMongoDB_Spark
MongoDB_SparkMat Keep
 

Similar to Introduction to MongoDB and its best practices (20)

Hands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYC
Hands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYCHands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYC
Hands on Big Data Analysis with MongoDB - Cloud Expo Bootcamp NYC
 
Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB.local Sydney: An Introduction to Document Databases with MongoDB
MongoDB.local Sydney: An Introduction to Document Databases with MongoDBMongoDB.local Sydney: An Introduction to Document Databases with MongoDB
MongoDB.local Sydney: An Introduction to Document Databases with MongoDB
 
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
 
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
 
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
MONGODB VS MYSQL: A COMPARATIVE STUDY OF PERFORMANCE IN SUPER MARKET MANAGEME...
 
MongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data scienceMongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data science
 
A Study on Mongodb Database.pdf
A Study on Mongodb Database.pdfA Study on Mongodb Database.pdf
A Study on Mongodb Database.pdf
 
A Study on Mongodb Database
A Study on Mongodb DatabaseA Study on Mongodb Database
A Study on Mongodb Database
 
Mongo db dhruba
Mongo db dhrubaMongo db dhruba
Mongo db dhruba
 
nosql [Autosaved].pptx
nosql [Autosaved].pptxnosql [Autosaved].pptx
nosql [Autosaved].pptx
 
SQL vs NoSQL, an experiment with MongoDB
SQL vs NoSQL, an experiment with MongoDBSQL vs NoSQL, an experiment with MongoDB
SQL vs NoSQL, an experiment with MongoDB
 
Performance analysis of MongoDB and HBase
Performance analysis of MongoDB and HBasePerformance analysis of MongoDB and HBase
Performance analysis of MongoDB and HBase
 
CMS Mongo DB
CMS Mongo DBCMS Mongo DB
CMS Mongo DB
 
Klevis Mino: MongoDB
Klevis Mino: MongoDBKlevis Mino: MongoDB
Klevis Mino: MongoDB
 
MongoDB presentation
MongoDB presentationMongoDB presentation
MongoDB presentation
 
Mongodb
MongodbMongodb
Mongodb
 
nodejs.pptx
nodejs.pptxnodejs.pptx
nodejs.pptx
 
MongoDB_Spark
MongoDB_SparkMongoDB_Spark
MongoDB_Spark
 
MongoDB
MongoDBMongoDB
MongoDB
 

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

Introduction to MongoDB and its best practices

  • 1. MongoDB and Best Practices BY - ASHISH RATHORE Unleash the Power of NoSQL with MongoDB - Scalable, Secure, and Agile
  • 2. Agenda KEY TOPICS DISCUSSED IN THIS PRESENTATION Introduction to MongoDB MongoDB Data Modeling Working with MongoDB MongoDB security Scaling MongoDB Real-world use cases of MongoDB Best practices for using MongoDB
  • 3. What is MongoDB? MongoDB is a NoSQL database management system that allows for flexible and scalable data storage and retrieval. Unlike traditional relational databases, MongoDB uses a document model to store data, allowing for easy and fast data access. MongoDB is an open-source project that is continuously developed and maintained by MongoDB, Inc. MongoDB offers powerful indexing capabilities, allowing for efficient querying of data.
  • 4. Relational databases are based on the traditional tabular model. Relational databases require a schema to be defined upfront Relational databases are vertically scalable, meaning that they are scaled up by adding more resources to a single server, which can be expensive and limited. Relational databases may be better suited for complex queries that require joining multiple tables. Relational databases are often used for transactional systems that require strict data consistency and integrity, such as banking or e- commerce applications. NoSQL databases use a variety of data models, such as document-based, key-value or graph db. NoSQL databases offer more flexible schema design. NoSQL databases, are horizontally scalable, meaning that they can be scaled out by adding more servers to the cluster, which allows for easier and more cost-effective scaling. NoSQL databases generally offer better performance and faster data access especially when dealing with large amounts of data. NoSQL databases are often used for applications that require flexible schema design, scalability, and fast data access, such as social media, gaming, or IoT applications. NOSQL(not only SQL ) SQL (Relational)
  • 5. RDBMS MONGODB Database Instance MongoDB Instance Schema Database Table Collection Row Document rowid _id Belongs to a Collection Has a field "_id" (unique within the document) Belongs to a Database Each JSON Document: Each Collection: Terminology
  • 6. 1 2 MongoDB Data Modeling: Designing for Flexibility and Performance SCHEMA DESIGN MongoDB offers flexible schema design, allowing for changes to the schema as the application evolves. DOCUMENT MODEL MongoDB uses a document model to store data, where each document represents a single object or entity. DATA STRUCTURE MongoDB supports nested and hierarchical data structures, allowing for more complex data models than traditional relational databases. INDEXING MongoDB provides powerful indexing capabilities to support fast data retrieval. BEST PRACTICES Denormalizing data, Avoiding overly complex, Using indexing strategically to support common queries. 3 4 5
  • 8. To insert a new document into a collection, you can use the insertOne() method: To update an existing document, you can use the updateOne() method: To delete a document, you can use the deleteOne() method: db.collection('users').insertOne({ name: 'John', age: 30, email: 'john@example.com' }); CRUD Operations CRUD OPERATIONS IN MONGODB ARE SIMILAR TO THOSE IN OTHER DATABASES, BUT USE DIFFERENT SYNTAX. db.collection('users').updateOne({ name: 'John' }, { $set: { age: 31 } }); db.collection('users').deleteOne({ name: 'John' })
  • 9. To find all documents in a collection with a specific field value, you can use the find() method with a query object To find documents that match multiple criteria, you can use the $and and $or operators: db.collection('users').find({ age: 30 }); Querying MongoDB Data MONGODB PROVIDES A RICH QUERY LANGUAGE THAT ALLOWS YOU TO SEARCH FOR DOCUMENTS BASED ON SPECIFIC CRITERIA db.collection('users').find({ $or: [{ age: 30 }, { age: 31 }] });
  • 10. To calculate the total number of users in a collection, you can use the $count operator: To group documents by a specific field and calculate aggregate values for each group, you can use the $group operator: db.collection('users').aggregate([{ $count: 'total_users' }]); Aggregation and Reporting MONGODB'S AGGREGATION FRAMEWORK PROVIDES A POWERFUL WAY TO ANALYZE DATA WITHIN COLLECTIONS db.collection('users').aggregate([{ $group: { _id: '$age', total: { $sum: 1 } } }]);
  • 12. MongoDB provides several mechanisms for authentication and authorization, including built-in and external authentication methods. Built-in authentication methods include username/password authentication, LDAP integration, and X.509 certificate authentication. Authorization in MongoDB is managed through roles, which control user access to specific databases, collections, and operations. Authentication and Authorization in MongoDB > use admin > db.createUser( { user: "justAUser", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "readWrite", db: "demo" }, { role: "read", db: "finances" } ] } )
  • 13. Enabling authentication and authorization for all users and applications accessing the database. Using strong passwords and rotating them regularly. Restricting network access to MongoDB servers and using encryption for data in transit. Implementing access controls and least privilege principles to limit user access to only what they need to do their job. Regularly applying patches and updates to the MongoDB software and underlying operating system. Best Practices for Securing MongoDB
  • 15. Vertical scaling Horizontal scaling Adding more resources, such as CPU, memory, or storage, to a single server. Adding more servers to a cluster, distributing data across them to increase performance and capacity. Scaling Options for MongoDB MONGODB PROVIDES SEVERAL OPTIONS FOR SCALING, INCLUDING:
  • 16. Sharding in MongoDB Sharding is a horizontal scaling strategy that involves dividing a large MongoDB dataset into smaller partitions called shards, which are distributed across multiple servers. MongoDB uses a sharded cluster to manage shard distribution and data movement, and provides several sharding strategies, including range-based, hash-based, and zone sharding. Sharding requires careful planning and configuration to ensure optimal performance, consistency, and availability. MongoDB also provides tools such as the balancer and shard status command to manage and monitor the sharded cluster.
  • 17. Real-World Use Cases of MongoDB These percentages represent the market share of MongoDB in each use case, indicating its popularity and adoption in those industries. E-commerce Applications 40% Social Media Applications 25% IoT and Big Data Applications 20% Mobile Applications 15%
  • 19. Hardware and Application Patterns Ensure your working set fits in RAM: As with most databases, MongoDB performs best when the working set (indexes and most frequently accessed data) fits in RAM. RAM size is the most important factor for hardware; other optimizations may not significantly improve the performance of the system if there is insufficient RAM. Use the db.serverStatus() command to view an estimate of the the current working set size. Test every query in your application with explain(): The number of documents returned The number of documents read Which indexes were used The number of index entries scanned How long the query took to resolve in milliseconds (when using the executionStats mode) MongoDB provides an explain plan capability that shows information about how a query will be, or was, resolved, including:
  • 20. Check the uptime The uptime field in the output of db.serverStatus() tells you how long the server has been running. If the server has been running for a long time without any issues, this is a good sign that things are running smoothly. Look at the connections The connections field in the output of db.serverStatus() tells you how many client connections are currently active. If the number of connections is consistently high, it may indicate that the server is under heavy load and may need more resources. Monitor the opcounters The opcounters field in the output of db.serverStatus() provides information about the number of operations performed on the server, including inserts, updates, deletes, and queries. If the opcounters are consistently high, it may indicate that the server is under heavy load and may need more resources. Check the network activity The network field in the output of db.serverStatus() provides information about the network activity of the server. If the network activity is consistently high, it may indicate that the server is under heavy load and may need more resources.
  • 21. Schema Design Store all data for a record in a single document: MongoDB provides ACID compliance at the document level. When data for a record is stored in a single document the entire record can be retrieved in a single seek operation, which is very efficient. In some cases it may not be practical to store all data in a single document, or it may negatively impact other operations. Avoid large documents The maximum size for documents in MongoDB is 16 MB. In practice, most documents are a few kilobytes or less. Rather than maintaining lists of records in a single document, instead make each record a document.
  • 22. Best Practices for Data Indexing, Security, and Scalability Boosting MongoDB Query Performance with Indexing Use indexing to improve query performance, but be mindful of the impact on write performance and index size. Proper use of indexes can greatly improve the performance of your MongoDB application. Securing Your MongoDB Deployment Implement proper authentication and authorization mechanisms to secure your MongoDB deployment, and follow other security best practices such as encrypting data at rest and in transit. Scaling MongoDB for Success Plan for scalability from the beginning, and consider horizontal scaling options such as sharding to handle increasing data volumes and user traffic.
  • 23. Backup and Recovery MongoDB Backup with mongodump Backing up MongoDB is a critical task to ensure data is safe and recoverable. MongoDB provides a command-line tool called "mongodump" to take a backup of data in BSON format. To create a backup using "mongodump", open the command prompt and run the following command: MongoDB Recovery with mongorestore To restore the backup created using "mongodump", MongoDB provides a command-line tool called "mongorestore". To restore a backup using "mongorestore", open the command prompt and run the following command: mongodump --host <hostname> --port <port> --db <database-name> --out <backup-directory> mongorestore --host <hostname> --port <port> --db <database-name> <backup-directory>
  • 24. What's Next in NOSQL As the field of data management continues to evolve, NoSQL databases are likely to play an increasingly important role in helping organizations to manage their growing volumes of data. Some of the key areas where NoSQL databases are likely to continue to innovate and expand in the coming years include: Advanced analytics and machine learning capabilities Greater support for distributed architectures and microservices Integration with emerging technologies such as blockchain and IoT Overall, the future looks bright for NoSQL databases, as organizations continue to seek out scalable, flexible, and cost- effective solutions for managing their data in the years to come.
  • 25. Do you have any questions?