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.