0% found this document useful (0 votes)
2 views13 pages

mongodb

MongoDB is a NoSQL database designed for managing large volumes of unstructured or semi-structured data using a flexible document-oriented model. It offers scalability through sharding, high performance, and powerful querying capabilities, making it suitable for applications like real-time analytics and IoT. However, it has limitations such as high memory usage, lack of traditional joins, and limited ACID compliance, making it less ideal for applications requiring complex relationships.

Uploaded by

misssyeda90
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)
2 views13 pages

mongodb

MongoDB is a NoSQL database designed for managing large volumes of unstructured or semi-structured data using a flexible document-oriented model. It offers scalability through sharding, high performance, and powerful querying capabilities, making it suitable for applications like real-time analytics and IoT. However, it has limitations such as high memory usage, lack of traditional joins, and limited ACID compliance, making it less ideal for applications requiring complex relationships.

Uploaded by

misssyeda90
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/ 13

INTRODUCTION TO MONGODB

INTRODUCTION

MongoDB is a NoSQL database designed for handling large amounts of unstructured or semi-
structured data efficiently. Unlike traditional relational databases that store data in tables with
fixed schemas, MongoDB uses a document-oriented model, storing data in JSON-like BSON
(Binary JSON) format. This provides flexibility, allowing documents to have varying structures
without predefined schemas.
One of MongoDB’s key advantages is scalability. It supports horizontal scaling through sharding,
which distributes data across multiple servers, making it ideal for handling big data and high-
traffic applications. Additionally, it offers replication through replica sets, ensuring high
availability and fault tolerance. MongoDB also provides a powerful query language, allowing
filtering, aggregation, and indexing for efficient data retrieval.
Unlike SQL databases, MongoDB does not support traditional joins, but it optimizes performance
by embedding related data within documents. Its flexibility makes it a popular choice for real-
time analytics, mobile applications, IoT, and content management systems. However, it may not
be the best fit for applications requiring complex relationships and strict ACID compliance, such
as financial systems.

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 1
INTRODUCTION TO MONGODB

Objectives

1. Schema-less (Flexible Structure) – No need to define a fixed schema, allowing dynamic


changes in data structure.
2. Document-Oriented – Stores data as JSON-like documents, making it easy to read and
scale.
3. Scalability – Supports horizontal scaling using sharding, making it ideal for big data
applications.
4. High Performance – Faster read and write operations compared to relational databases in
many cases.
5. Indexing – Supports various types of indexes for efficient data retrieval.
6. Aggregation Framework – Provides powerful querying and data processing capabilities.
7. Replication – Ensures high availability with replica sets that keep multiple copies of data.
Use Cases of MongoDB:
 Big Data & Real-time Analytics
 IoT (Internet of Things) applications
 E-commerce Platforms
 Content Management Systems (CMS)
 Mobile & Web Application

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 2
INTRODUCTION TO MONGODB

Advantages and Disadvantages of MongoDB

Advantages of MongoDB:
1. Schema Flexibility (NoSQL Structure)
o No need for a fixed schema, allowing dynamic changes to documents.
o Ideal for handling unstructured or semi-structured data.
2. Scalability (Horizontal Scaling via Sharding)
o Can handle large volumes of data efficiently.
o Uses sharding (distributing data across multiple servers) for scaling.
3. High Performance
o Faster read/write operations due to its document-based storage.
o Uses indexing for efficient data retrieval.
4. Replication & High Availability
o Uses Replica Sets to ensure data redundancy and availability in case of failures.
5. Rich Query Language & Aggregation
o Supports powerful queries like filtering, sorting, and data transformations using the
aggregation framework.
6. Easy Integration with Modern Applications
o Works well with JavaScript (Node.js), Python, and other languages.
o Ideal for microservices and cloud-based applications.
7. Geospatial and Real-Time Data Handling
o Supports geospatial queries, making it useful for location-based apps.
o Suitable for real-time applications like IoT, social media analytics, and gaming.

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 3
INTRODUCTION TO MONGODB

Disadvantages of MongoDB:
1. High Memory Usage
o Stores duplicate keys due to its denormalized structure, leading to increased
storage consumption.
o Indexes consume extra memory.
2. No ACID Compliance (Before Version 4.0)
o Earlier versions lacked full ACID (Atomicity, Consistency, Isolation,
Durability) transactions, making MongoDB less suitable for applications
requiring strong consistency (e.g., banking systems).
o Later versions (4.0+) introduced multi-document transactions, but it's still not as
robust as traditional SQL databases.
3. Complex Joins & Relationships
o Unlike SQL, MongoDB does not support traditional JOIN operations efficiently.
o Data redundancy increases when handling complex relationships.
4. Limited Support for Complex Transactions
o Not ideal for applications requiring multi-step, relational transactions.

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 4
INTRODUCTION TO MONGODB

Installing MongoDB

The installation process depends on your operating system. Below are the steps for Windows
Windows Installation (Using MSI Installer)
1️Download MongoDB Community Edition
 Go to the official MongoDB website: MongoDB Download Center
 Select Windows, then download the MSI Installer.
2️Run the Installer
 Open the downloaded MSI file and follow the installation wizard.
 Choose Complete Installation.
3️Configure MongoDB
 Enable the option "Run MongoDB as a service" (recommended).
4️Set Up Data Directory
 By default, MongoDB uses C:\data\db to store data.
 If it doesn’t exist, create it manually:
mkdir C:\data\db
5️Start MongoDB Service
 Open Command Prompt (cmd) as Administrator and run:
net start MongoDB
 To stop MongoDB, run:
net stop MongoDB
6️Verify Installation
 Open Command Prompt and type:
mongo
 If MongoDB starts successfully, the installation is complete.

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 5
INTRODUCTION TO MONGODB

Creating a Database in MongoDB


Steps to Create a Database in MongoDB

1️Open MongoDB Shell


 If you're using MongoDB shell, run:
 Mongosh

2️Create or Switch to a Database


 Use the use <database_name> command:
 use mydatabase
 If the database does not exist, MongoDB will create it when you insert data.

3️Verify the Database


 To check the current database:
 db
 To list all databases:
 show dbs
 Note: The new database won’t appear in show dbs until it has at least one collection with
data.

4️Create a Collection (Optional)


 MongoDB will automatically create a collection when you insert a document, but you can
also create one manually:
 db.createCollection("students")

5️Insert Data (Creates Database Automatically)


 Add a document to the collection:
 db.students.insertOne({ name: "Alice", age: 22, course: "Computer Science" })
 Now, if you run show dbs, "mydatabase" will appear.

6️Check Available Collections


show collections

Example: Creating a Database and Adding Data


use schoolDB
db.students.insertOne({ name: "John", age: 20, course: "Physics" })
show dbs
show collections

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 6
INTRODUCTION TO MONGODB

Retrieving Data from MongoDB


Once data is inserted into a collection, you can retrieve it using queries in MongoDB.
Steps to Retrieve Data in MongoDB
1️Open MongoDB Shell
mongosh
If you're using the older Mongo shell, use:
mongo
2️Switch to Your Database
use mydatabase
3️List Available Collections
show collections
This will display all collections in the database.
4️Retrieve Data Using find()
 To get all documents in a collection:
 db.students.find()
 To format results in a readable way:
 db.students.find().pretty()
Querying Data with Filters
You can filter documents using query conditions inside find({}).
1️Retrieve a Single Document
db.students.findOne()
2️Retrieve Data with a Condition
 Find students where age = 22:
 db.students.find({ age: 22 })
 Find students in Computer Science course:
 db.students.find({ course: "Computer Science" })
3️Retrieve Data with Multiple Conditions

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 7
INTRODUCTION TO MONGODB

 Find students with age = 22 and course = "Computer Science":


 db.students.find({ age: 22, course: "Computer Science" })
 Using comparison operators:
o Students older than 20:
o db.students.find({ age: { $gt: 20 } })
o Students aged 20 to 25:
o db.students.find({ age: { $gte: 20, $lte: 25 } })
4️ Retrieve Specific Fields
 Show only names of students:
 db.students.find({}, { name: 1, _id: 0 })
o 1 includes the field, _id: 0 excludes the default _id field.
5 Sorting Results
 Sort by age ascending:
 db.students.find().sort({ age: 1 })
 Sort by name descending:
 db.students.find().sort({ name: -1 })
6️ Limit & Skip Results
 Limit the result to 3 students:
 db.students.find().limit(3)
 Skip the first 2 students:
 db.students.find().skip(2)
Example: Full Query
Retrieve students older than 20, show only names and courses, sorted by name in descending
order, limit to 2 results:
db.students.find({ age: { $gt: 20 } }, { name: 1, course: 1, _id: 0 }).sort({ name: -1 }).limit(2)

Results
Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),
Sharnbasva University, Kalaburagi page 8
INTRODUCTION TO MONGODB

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 9
INTRODUCTION TO MONGODB

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 10
INTRODUCTION TO MONGODB

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 11
INTRODUCTION TO MONGODB

Conclusion

MongoDB is a powerful and flexible NoSQL database that is well-suited for handling large
volumes of unstructured or semi-structured data. Unlike traditional relational databases, it uses a
document-oriented model, allowing for dynamic schema changes and easy scalability. Its ability
to distribute data across multiple servers through sharding makes it an excellent choice for
applications that require high availability and performance. Additionally, MongoDB supports
powerful querying capabilities, indexing, and an aggregation framework, making data retrieval
and manipulation efficient.
One of MongoDB’s biggest advantages is its scalability, as it can handle massive datasets by
distributing data across multiple nodes. It also provides replication through replica sets, ensuring
fault tolerance and high availability. However, MongoDB has some limitations, such as higher
memory usage due to data redundancy, lack of built-in join operations, and limited ACID
compliance compared to SQL databases. These factors make it less suitable for applications that
require complex relationships or strict transactional consistency, such as banking systems.
Overall, MongoDB is ideal for applications like real-time analytics, IoT, content management
systems, and e-commerce platforms. While it may not be the best choice for highly relational
data, its performance, flexibility, and scalability make it a preferred database for modern, high-
performance applications.

REFERENCE
Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),
Sharnbasva University, Kalaburagi page 12
INTRODUCTION TO MONGODB

1. .https://www.geeksforgeeks.orgMastering MongoDB 7.0


Marko Aleksendric, Arek Borucki, Leandro Domingues, Malak Abu Hammad, Elie Hannouch,
Rajesh Nair, Rachelle Palmer
2. https://www.w3schools.com
3. https://openai.com

Dept of CSE, Faculty Of Engineering and Technology (Exclusively for Women),


Sharnbasva University, Kalaburagi page 13

You might also like