MST UNIT-5
MST UNIT-5
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'
}
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.
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:
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.
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.
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:
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:
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.
- **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)
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'