0% found this document useful (0 votes)
29 views

MST UNIT-5

MongoDB is a popular open-source NoSQL database that uses a document-oriented structure and stores data in BSON format. Key features include horizontal scalability, high availability through replication, and a flexible schema-less design. The document structure allows for easy data retrieval, while its architecture includes components like drivers, storage engines, and security measures to ensure efficient data management.

Uploaded by

royalempire669
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

MST UNIT-5

MongoDB is a popular open-source NoSQL database that uses a document-oriented structure and stores data in BSON format. Key features include horizontal scalability, high availability through replication, and a flexible schema-less design. The document structure allows for easy data retrieval, while its architecture includes components like drivers, storage engines, and security measures to ensure efficient data management.

Uploaded by

royalempire669
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

nit

UNIT-5
MongoDB:
 MongoDB the most popular NoSQL database, is an open-source document-
oriented database. The term ‘NoSQL’ means ‘non-relational‘.
 It means that MongoDB isn’t based on the table-like relational
database structure but provides an altogether different mechanism for
the storage and retrieval of data. This format of storage is
called BSON ( similar to JSON format).
A simple MongoDB document Structure:
{
title: 'Geeksforgeeks',
by: 'Harshit Gupta',
url: 'https://www.geeksforgeeks.org',
type: 'NoSQL'
}

Key Features of MongoDB


 Document-oriented Database
 Stores data in BSON-like documents.
 Schema Less database.
 It provides horizontal scalability with the help of sharding.
 It provides high availability and redundancy with the help of replication.
 It allows one to perform operations on the grouped data and get a single result or
computed result.
 It has very high performance.

MongoDB Architecture and its Components


MongoDB's architecture design involves several important
parts that work together to create a strong and flexible
database system. these are the following MongoDB's
architecture

MongoDB-Architecture
1. Drivers & Storage Engine
MongoDB store the data on the server but that data we will try
to retrieve from our application. So that time how the
communication is happening between our application and
MongoDB server.
Any application which is written in python, .net and java or
any kind of frontend application, these application are trying
to access the data from these physical storage in server. First
they will interact with driver which will communicate with
MongoDB server. What happen is once the request is going
from the frontend application through the driver then driver
will change appropriate query by using query engine and then
the query will get executed in MongoDB data model. Left
side is security which provides security to the database that
who will access the data and right side is management this
management will manage all these things.
Drivers
Drivers are client libraries that offer interfaces and methods
for applications to communicate with MongoDB databases.
Drivers will handle the translation of documents between
BSON objects and mapping application structures.

.NET, Java, JavaScript, Node.js, Python, etc are some of the


widely used drives supported by MongoDB.

Storage Engine
The storage engine significantly influences the performance
of applications, serving as an intermediary between the
MongoDB database and persistent storage, typically disks.
MongoDB supports different storage engines:

MMAPv1 – It is a traditional storage engine based on memory


mapped files. This storage engine is optimized for workloads
with high volumes of read operations, insertions, and in-place
updates. It uses B-tress to store indexes. Storage Engine
works on multiple reader single writer lock. A user cannot
have two write calls to be processes in parallel on the same
collection. It is fast for reads and slow for writes.
Wired Tiger – Default Storage Engine starts from MongoDB
3version. No locking Algorithms like hash pointer. It yields
7x-10x better write operations and 80% of the file system
compression than MMAP.
InMemory – Instead of storing documents on disk, the engine
uses in-memory for more predictable data latencies. It uses
50% of physical RAM minimum 1 GB as default. It requires
all its data. When dealing with large datasets, the in-memory
engine may not be the most suitable choice.
2. Security
Authentication
Authorization
Encryption on data
Hardening (Ensure only trusted hosts have access)
3. MongoDB Server
It serves as the central element and is in charge of
maintaining, storing, and retrieving data from the database
through a number of interfaces. The system's heart is the
MongoDB server. Each mongod server instance is in charge
of handling client requests, maintaining data storage, and
performing database operations. Several mongod instances
work together to form a cluster in a typical MongoDB setup.

4. MongoDB Shell
For dealing with MongoDB databases, MongoDB provides
the MongoDB Shell command-line interface (CLI) tool. The
ability to handle and query MongoDB data straight from the
terminal is robust and flexible. After installing MongoDB, you
may access the MongoDB Shell, often known as mongo. It
interacts with the database using JavaScript-based syntax.
Additionally, it has built-in help that shows details about
possible commands and how to use them.
5. Data Storage in MongoDB
5.1 Collections
A database can contain as many collections as it wishes, and
MongoDB stores data inside collections.

As an example, a database might contain three collections a


user's collection, a blog post collection, and a comments
collection. The user collection would hold user data and
documents, the blog post collection would hold blog posts and
documents, and the comments collection would hold
documents related to comments. This would allow for the
easy retrieval of all the documents from a single collection.
5.2 Documents
Documents themselves represent the individual records in a
specific collection.

For example inside the blog posts collection we'd store a lot of
blog post documents and each one represents a single blog
post now the way that data is structured inside a document
looks very much like a JSON object with key value pairs but
actually it's being stored as something called BSON which is
just binary JSON.
6. Indexes
Indexes are data structures that make it simple to navigate
across the collection's data set. They help to execute queries
and find documents that match the query criteria without a
collection scan.
These are the following different types of indexes in
MongoDB:
6.1 Single field
MongoDB can traverse the indexes either in the ascending or
descending order for single-field index
db.students.createIndex({“item”:1})
In this example, we are creating a single index on the item
field and 1 here represents the filed is in ascending order.
A compound index in MongoDB contains multiple single filed
indexes separated by a comma. MongoDB restricts the
number of fields in a compound index to a maximum of 31.
db.students.createIndex({“item”: 1, “stock”:1})
Here, we create a compound index on item: 1, stock:1

6.2 Multi-Key
When indexing a filed containing an array value, MongoDB
creates separate index entries for each array component.
MongoDB allows you to create multi-key indexes for arrays
containing scalar values, including strings, numbers, and
nested documents.
db.students.createIndex({<filed>: <1 or -1>})
6.3 Geo Spatial
Two geospatial indexes offered by MongoDB are called 2d
indexes and 2d sphere indexes. These indexes allow us to
query geospatial data. On this case, queries intended to locate
data stored on a two-dimensional plane are supported by the
2d indexes. On the other hand, queries that are used to locate
data stored in spherical geometry are supported by 2D sphere
indexes.
6.4 Hashed
To maintain the entries with hashes of the values of the
indexed field we use Hash Index. MongoDB supports hash
based sharding and provides hashed indexes.
db.<collection>.createIndex( { item: “hashed” } )
7. Replication
Within a MongoDB cluster, data replication entails keeping
several copies of the same data on various servers or nodes.
Enhancing data availability and dependability is the main
objective of data replication. A replica may seamlessly replace
a failing server in the cluster to maintain service continuity
and data integrity.

Primary Node (Primary Replica): In a replica set, the primary


node serves as the main source for all write operations. It's the
only node that accepts write requests. The main node is where
all data modifications begin and are implemented initially.
Secondary Nodes: Secondary nodes duplicate data from the
primary node (also known as secondary replicas). They are
useful for dispersing read workloads and load balancing since
they are read-only and mostly utilized for read activities.
8. Sharding
Sharding is basically horizontal scaling of databases as
compared to the traditional vertical scaling of adding more
CPUS and ram to the current system.

For example, you have huge set of files you might segregate it
into smaller sets for ease. Similarly what mongo database
does is it segregates its data into smaller chunks to improve
the efficiency.
Creating a Database and Collection in MongoDB
Creating a database and collection in MongoDB is a simple process that
allows you to efficiently organize and manage your data. MongoDB’s
flexible, schema-less nature enables you to easily create and modify
databases and collections as your application grows. Below are the
steps of creating a database, adding collections, and inserting
documents into collections.
Step 1: Create a Database In MongoDB
In MongoDB, databases are not created explicitly until you insert some
data into them. The use command is employed to either create a new
database or switch to an existing one. When you use this command with
a database name, MongoDB will switch to that database. If it doesn’t
exist yet, MongoDB will create it once we insert some data.
Syntax:
use database_name
Example
To create or switch to a database called gfgDB, you would use the
following command:
Explanation:
 If the gfgDBdatabase doesn’t exist, MongoDB will create it automatically
once you insert data into it.
 If the database already exists, the use command simply switches the
context to that database.
 Note that MongoDB doesn’t actually create the database until you store
some data.
Check Existing Databases:
To see all the databases that already exist in your MongoDB instance,
use the show dbs command:
show dbs
Example Output:

This command lists all the databases in your MongoDB instance,


including their respective sizes. To check the current database in use, we
can use the db command:
db
This will return the name of the currently selected database, e.g., gfgDB
Step 2: Create a Collection in MongoDB
In MongoDB, a collection is a group of documents. Collections are
similar to tables in relational databases. However, collections do not
enforce schema, allowing allowing you to store documents with varying
structures in the same collection. This flexibility is one of the key
features of MongoDB.
To explicitly create a collection in MongoDB, you can use
the createCollection() method. However, it’s important to note that
collections are also automatically created when you insert the first
document into them.
Syntax:
db.createCollection(‘ collection_name’ );
Example:
To create a collection called Student, you would use the following
command:
db.createCollection('Student');
Explanation:
 The createCollection() method is used to explicitly create a collection
in MongoDB.
 If you attempt to insert a document into a collection that doesn’t exist,
MongoDB will automatically create it for you without needing
the createCollection() method.
For example, running the following command will automatically create
the Student collection if it doesn’t exist:
db.Student.insertOne({Name: "Om", age: 19})
Output

Deploying Applications: Web hosting & Domains

Deploying applications" refers to the process of making a web application


accessible to users by placing it on a web server, which requires both a
domain name (to identify the website) and web hosting (a service that
stores and serves the application files) on a server; essentially, connecting
your developed application to a live website accessible through a specific
domain name.
Key points about deploying applications:
 Domain name:
A unique identifier for your website, like "example.com", which users type
in their browser to access your application.

 Web hosting:
A service that provides the physical or virtual servers where your
application files are stored and made accessible to users through the
internet.
Steps involved in deploying an application:
 Develop the application: Write the code for your web application using
programming languages like HTML, CSS, JavaScript, etc.
 Choose a web hosting provider: Select a hosting service based on your
needs like shared hosting, cloud hosting, dedicated server, etc.
 Register a domain name: Purchase a domain name from a domain
registrar.
 Connect domain to hosting: Configure DNS settings to point your domain
name to the web hosting server.
 Upload application files: Transfer your application files to the hosting
server's designated directory.
 Database setup (if needed): Create and configure any required databases
for your application.
 Testing and deployment: Thoroughly test your application on the live
server before making it publicly accessible.
Popular web hosting platforms:
 Shared hosting:
Affordable option for basic websites, where multiple websites share the
same server resources.
 Cloud hosting:
Scalable option where your application runs on a network of virtual
servers.
 AWS (Amazon Web Services):
A comprehensive cloud platform with various services including web
hosting.
 Google Cloud Platform:
Another robust cloud hosting option with various features.
Deployment Using Cloud Platforms:

Steps to Deploy MongoDB on a Cloud Platform

1. **Choose a Cloud Provider**: Select a cloud provider that supports MongoDB,


such as **MongoDB Atlas**, **Google Cloud**, **AWS**, or **Azure**.

2. **Create an Account**: Sign up for an account with your chosen cloud provider.

3. **Set Up a Cluster**: Use the cloud provider's interface to create a new MongoDB
cluster. You can choose the type of cluster based on your needs (e.g., dedicated,
shared, or flex clusters).

4. **Configure Cluster Settings**: Define your cluster's configuration, such as the tier,
region, and security settings.

5. **Connect to Your Cluster**: Obtain the connection string and use it to connect to
your MongoDB cluster from your application.

6. **Load Data**: Import your data into the cluster. You can load sample data
provided by the cloud provider or upload your own data.

7. **Manage Access**: Set up user credentials and IP access lists to control who can
access your database.

8. **Deploy Your Application**: Integrate your application with the MongoDB cluster
using the connection string.

Benefits of Using a Cloud Platform for MongoDB

- **Scalability**: Easily scale your database to handle increased traffic and data
volume.
- **High Availability**: Ensure your database is always available with built-in
redundancy and failover mechanisms.
- **Security**: Benefit from the cloud provider's security features, such as encryption
and access control.
- **Maintenance**: Reduce the need for manual maintenance as the cloud provider
handles infrastructure management.

MongoDB examples:
1. Connecting to MongoDB
python
CopyEdit
from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"] # Database
collection = db["users"] # Collection

2. Inserting Documents

Insert One
python
CopyEdit
user = {"name": "Alice", "age": 25, "city": "New York"}
collection.insert_one(user)

Insert Many
python
CopyEdit
users = [
{"name": "Bob", "age": 30, "city": "Chicago"},
{"name": "Charlie", "age": 28, "city": "San Francisco"}
]
collection.insert_many(users)

3. Querying Documents

Find One
python
CopyEdit
user = collection.find_one({"name": "Alice"})
print(user)

Find Many
python
CopyEdit
for user in collection.find({"city": "Chicago"}):
print(user)

Using Query Operators


python
CopyEdit
# Find users older than 25
for user in collection.find({"age": {"$gt": 25}}):
print(user)

4. Updating Documents

Update One
python
CopyEdit
collection.update_one({"name": "Alice"}, {"$set": {"age": 26}})

Update Many
python
CopyEdit
collection.update_many({"city": "Chicago"}, {"$set": {"city": "Houston"}})
5. Deleting Documents

Delete One
python
CopyEdit
collection.delete_one({"name": "Charlie"})

Delete Many
python
CopyEdit
collection.delete_many({"city": "Houston"})

6. Advanced Queries

Sorting
python
CopyEdit
for user in collection.find().sort("age", -1): # Sort by age descending
print(user)

Limiting Results
python
CopyEdit
for user in collection.find().limit(2):
print(user)

Using Regex
python
CopyEdit
for user in collection.find({"name": {"$regex": "^A"}}):
print(user) # Names starting with 'A'

7. Indexing for Performance


python
CopyEdit
collection.create_index([("name", 1)]) # 1 for ascending, -1 for
descending

You might also like