SlideShare a Scribd company logo
By : Manik Gupta
22BCE1243
mongoDB.
● Document-Oriented Storage: MongoDB stores data in flexible, JSON-like documents,
making it easier to work with complex data structures and unstructured data.
● Schema Flexibility: Unlike traditional relational databases, MongoDB doesn't require a
predefined schema. Fields in a document can vary, allowing dynamic changes to data
structures.
● Horizontal Scalability: MongoDB supports horizontal scaling through sharding,
distributing data across multiple servers to handle large volumes of data and traffic.
● High Performance: With features like indexing, in-memory storage, and replication,
MongoDB provides efficient read and write operations for high-performance applications.
● Rich Query Language: MongoDB offers a powerful query language for filtering,
aggregating, and transforming data, along with support for geospatial queries and full-text
search.
Introduction to MongoDB – A NoSQL Database
Introduction to MongoDB – A NoSQL Database
Introduction to MongoDB – A NoSQL Database
WORKING OF mongoDB
1. Database
● A database in MongoDB is a container for collections.
● Each MongoDB instance can have multiple databases, and each database is independent of the others.
● A database is created when you insert data or collections into it, and it can be deleted by dropping the database.
2. Collection
● A collection is a group of MongoDB documents, analogous to a table in relational databases.
● Collections do not enforce any schema, meaning documents within the same collection do not need to have the
same structure (fields and data types).
● Collections can store documents of any type or format, and the data can evolve over time.
● Schema-less: No rigid structure or rules on how documents are stored.
3. Document
● A document in MongoDB is the basic unit of storage, similar to a row in a relational database.
● Documents are stored in a BSON (Binary JSON) format, which is an extension of JSON. BSON supports more
data types, like binary data and ObjectId (for unique identification), which JSON does not support.
● A document is a set of key-value pairs (fields and values).
Introduction to MongoDB – A NoSQL Database
Creating a Database
The use<database_name> command makes a new database if it doesn't exist
else it uses the database.
Inserting a Document Into A Collection
● insertOne()
db.<collection>.insertOne()
● insertMany()
db.<collection>.insertMany(
<document1>,
<document2>
)
Data Types Supported By mongoDB
MongoDB supports a variety of data types, including:
String: A basic and common data type that can store international characters as UTF-8 BSON strings
Numeric values: MongoDB supports a variety of numeric data types, including:
Integer: Stores numeric values
Double: Stores numeric numbers as 64-bit IEEE 754 floating point
Date: Stores the current date or time as a 64-bit integer
Timestamp: Represents time as a sequence of characters
Object: Stores embedded documents as a series of nested documents
Array: An array data type
Binary data: A binary data type
Finding Documents
Using the find Operator
When given equality with an _id field, the find() command will return the specified document that matches the _id.
Here's an example:
Using the $in Operator
Use the $in operator to select documents where the value of a field equals any value in the specified array. Here's an example:
$gt
Use the $gt operator to match documents with a field greater than the given value. For example:
1
db.sales.find({ "items.price": { $gt: 50}})
$lt
Use the $lt operator to match documents with a field less than the given value. For example:
1
db.sales.find({ "items.price": { $lt: 50}})
$lte
Use the $lte operator to match documents with a field less than or equal to the given value. For example:
1
db.sales.find({ "customer.age": { $lte: 65}})
$gte
Use the $gte operator to match documents with a field greater than or equal to the given value. For example:
1
db.sales.find({ "customer.age": { $gte: 65}})
Find a Document by Using Implicit $and
Use implicit $and to select documents that match multiple expressions. For example:
db.collection('inventory').find({
status: 'A',
qty: { $lt: 30 }
});
Find a Document by Using the $or Operator
Use the $or operator to select documents that match at least one of the included expressions. For example:
db.routes.find({
$or: [{ dst_airport: "SEA" }, { src_airport: "SEA" }],
})
Updating Documents
$set
The $set operator replaces the value of a field with the specified value, as shown in the following code:
db.podcasts.updateOne({
_id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8"),
},{
$set: {
subscribers: 98562,
},
}
)
$push
The $push operator adds a new value to the hosts array field. Here's an example:
db.podcasts.updateOne(
{ _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8") },
{ $push: { hosts: "Nic Raboy" } }
)
upsert
The upsert option creates a new document if no documents match the filtered criteria. Here's an example:
db.podcasts.updateOne(
{ title: "The Developer Hub" },
{ $set: { topics: ["databases", "MongoDB"] } },
{ upsert: true }
)
Deleting Documents in mongoDB
Delete One Document
The following code shows an example of the deleteOne() method:
db.podcasts.deleteOne({ _id: Objectid("6282c9862acb966e76bbf20a") })
Delete Many Documents
The following code shows an example of the deleteMany() method:
db.podcasts.deleteMany({category: “crime”})
Sorting and Limiting Query Results
%sort:
Use cursor.sort() to return query results in a specified order. Within the parentheses of sort(),
include an object that specifies the field(s) to sort by and the order of the sort. Use 1 for ascending order,
and -1 for descending order.
Syntax:
db.collection.find(<query>).sort(<sort>)
Example
// Return data on all music companies, sorted alphabetically from A to Z. Ensure
consistent sort order
db.companies.find({ category_code: "music" }).sort({ name: 1, _id: 1 });
Limiting Results
Use cursor.limit() to return query results in a specified order. Within the parentheses of limit(), specify the
maximum number of documents to return.
Syntax:
db.companies.find(<query>).limit(<number>)
Example:
db.companies
.find({ category_code: "music" })
.sort({ number_of_employees: -1, _id: 1 })
.limit(3);
COUNT DOCUMENTS
Use db.collection.countDocuments() to count the number of documents
Syntax:
db.collection.countDocuments( <query>, <options> )
// Count number of trips over 120 minutes by subscribers
db.trips.countDocuments({ tripduration: { $gt: 120 }, usertype: "Subscriber" })
Introduction to MongoDB – A NoSQL Database

More Related Content

PPTX
Introduction to MongoDB
PPTX
Mongo db
PDF
MongoDB NoSQL database a deep dive -MyWhitePaper
PPTX
MongoDB introduction features -presentation - 2.pptx
PDF
Mongodb Introduction
PDF
3-Mongodb and Mapreduce Programming.pdf
PDF
MongoDB - An Introduction
PPTX
NOSQL and MongoDB Database
Introduction to MongoDB
Mongo db
MongoDB NoSQL database a deep dive -MyWhitePaper
MongoDB introduction features -presentation - 2.pptx
Mongodb Introduction
3-Mongodb and Mapreduce Programming.pdf
MongoDB - An Introduction
NOSQL and MongoDB Database

Similar to Introduction to MongoDB – A NoSQL Database (20)

PPTX
Query Optimization in MongoDB
PPTX
mongodb introduction11111111111111111111
PDF
MongoDB Interview Questions PDF By ScholarHat
PPTX
PPTX
MongoDB using Grails plugin by puneet behl
PPTX
fard car.pptx
PPTX
Mongo db Quick Guide
PPT
MongoDB
PPTX
Introduction To MongoDB
PPTX
UNIT-1 MongoDB.pptx
PDF
MongoDB
PPTX
Mongo DB Presentation
PDF
MongoDB Interview Questions PDF By ScholarHat
PDF
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
PPTX
MongoDB - An Introduction
PPTX
Mongo Nosql CRUD Operations
PPTX
MongoDB_ppt.pptx
PDF
Mongodb By Vipin
PDF
Storage dei dati con MongoDB
PPTX
MongoDB-presentation.pptx
Query Optimization in MongoDB
mongodb introduction11111111111111111111
MongoDB Interview Questions PDF By ScholarHat
MongoDB using Grails plugin by puneet behl
fard car.pptx
Mongo db Quick Guide
MongoDB
Introduction To MongoDB
UNIT-1 MongoDB.pptx
MongoDB
Mongo DB Presentation
MongoDB Interview Questions PDF By ScholarHat
MongoDB.pdf54teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer
MongoDB - An Introduction
Mongo Nosql CRUD Operations
MongoDB_ppt.pptx
Mongodb By Vipin
Storage dei dati con MongoDB
MongoDB-presentation.pptx
Ad

Recently uploaded (20)

PPTX
web development for engineering and engineering
PDF
PPT on Performance Review to get promotions
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
composite construction of structures.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Mechanical Engineering MATERIALS Selection
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
web development for engineering and engineering
PPT on Performance Review to get promotions
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Operating System & Kernel Study Guide-1 - converted.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
composite construction of structures.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Internet of Things (IOT) - A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Model Code of Practice - Construction Work - 21102022 .pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CH1 Production IntroductoryConcepts.pptx
additive manufacturing of ss316l using mig welding
Foundation to blockchain - A guide to Blockchain Tech
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mechanical Engineering MATERIALS Selection
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Ad

Introduction to MongoDB – A NoSQL Database

  • 1. By : Manik Gupta 22BCE1243
  • 2. mongoDB. ● Document-Oriented Storage: MongoDB stores data in flexible, JSON-like documents, making it easier to work with complex data structures and unstructured data. ● Schema Flexibility: Unlike traditional relational databases, MongoDB doesn't require a predefined schema. Fields in a document can vary, allowing dynamic changes to data structures. ● Horizontal Scalability: MongoDB supports horizontal scaling through sharding, distributing data across multiple servers to handle large volumes of data and traffic. ● High Performance: With features like indexing, in-memory storage, and replication, MongoDB provides efficient read and write operations for high-performance applications. ● Rich Query Language: MongoDB offers a powerful query language for filtering, aggregating, and transforming data, along with support for geospatial queries and full-text search.
  • 7. 1. Database ● A database in MongoDB is a container for collections. ● Each MongoDB instance can have multiple databases, and each database is independent of the others. ● A database is created when you insert data or collections into it, and it can be deleted by dropping the database. 2. Collection ● A collection is a group of MongoDB documents, analogous to a table in relational databases. ● Collections do not enforce any schema, meaning documents within the same collection do not need to have the same structure (fields and data types). ● Collections can store documents of any type or format, and the data can evolve over time. ● Schema-less: No rigid structure or rules on how documents are stored. 3. Document ● A document in MongoDB is the basic unit of storage, similar to a row in a relational database. ● Documents are stored in a BSON (Binary JSON) format, which is an extension of JSON. BSON supports more data types, like binary data and ObjectId (for unique identification), which JSON does not support. ● A document is a set of key-value pairs (fields and values).
  • 9. Creating a Database The use<database_name> command makes a new database if it doesn't exist else it uses the database.
  • 10. Inserting a Document Into A Collection ● insertOne() db.<collection>.insertOne() ● insertMany() db.<collection>.insertMany( <document1>, <document2> )
  • 11. Data Types Supported By mongoDB MongoDB supports a variety of data types, including: String: A basic and common data type that can store international characters as UTF-8 BSON strings Numeric values: MongoDB supports a variety of numeric data types, including: Integer: Stores numeric values Double: Stores numeric numbers as 64-bit IEEE 754 floating point Date: Stores the current date or time as a 64-bit integer Timestamp: Represents time as a sequence of characters Object: Stores embedded documents as a series of nested documents Array: An array data type Binary data: A binary data type
  • 12. Finding Documents Using the find Operator When given equality with an _id field, the find() command will return the specified document that matches the _id. Here's an example: Using the $in Operator Use the $in operator to select documents where the value of a field equals any value in the specified array. Here's an example:
  • 13. $gt Use the $gt operator to match documents with a field greater than the given value. For example: 1 db.sales.find({ "items.price": { $gt: 50}}) $lt Use the $lt operator to match documents with a field less than the given value. For example: 1 db.sales.find({ "items.price": { $lt: 50}}) $lte Use the $lte operator to match documents with a field less than or equal to the given value. For example: 1 db.sales.find({ "customer.age": { $lte: 65}}) $gte Use the $gte operator to match documents with a field greater than or equal to the given value. For example: 1 db.sales.find({ "customer.age": { $gte: 65}})
  • 14. Find a Document by Using Implicit $and Use implicit $and to select documents that match multiple expressions. For example: db.collection('inventory').find({ status: 'A', qty: { $lt: 30 } }); Find a Document by Using the $or Operator Use the $or operator to select documents that match at least one of the included expressions. For example: db.routes.find({ $or: [{ dst_airport: "SEA" }, { src_airport: "SEA" }], })
  • 15. Updating Documents $set The $set operator replaces the value of a field with the specified value, as shown in the following code: db.podcasts.updateOne({ _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8"), },{ $set: { subscribers: 98562, }, } )
  • 16. $push The $push operator adds a new value to the hosts array field. Here's an example: db.podcasts.updateOne( { _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8") }, { $push: { hosts: "Nic Raboy" } } ) upsert The upsert option creates a new document if no documents match the filtered criteria. Here's an example: db.podcasts.updateOne( { title: "The Developer Hub" }, { $set: { topics: ["databases", "MongoDB"] } }, { upsert: true } )
  • 17. Deleting Documents in mongoDB Delete One Document The following code shows an example of the deleteOne() method: db.podcasts.deleteOne({ _id: Objectid("6282c9862acb966e76bbf20a") }) Delete Many Documents The following code shows an example of the deleteMany() method: db.podcasts.deleteMany({category: “crime”})
  • 18. Sorting and Limiting Query Results %sort: Use cursor.sort() to return query results in a specified order. Within the parentheses of sort(), include an object that specifies the field(s) to sort by and the order of the sort. Use 1 for ascending order, and -1 for descending order. Syntax: db.collection.find(<query>).sort(<sort>) Example // Return data on all music companies, sorted alphabetically from A to Z. Ensure consistent sort order db.companies.find({ category_code: "music" }).sort({ name: 1, _id: 1 });
  • 19. Limiting Results Use cursor.limit() to return query results in a specified order. Within the parentheses of limit(), specify the maximum number of documents to return. Syntax: db.companies.find(<query>).limit(<number>) Example: db.companies .find({ category_code: "music" }) .sort({ number_of_employees: -1, _id: 1 }) .limit(3);
  • 20. COUNT DOCUMENTS Use db.collection.countDocuments() to count the number of documents Syntax: db.collection.countDocuments( <query>, <options> ) // Count number of trips over 120 minutes by subscribers db.trips.countDocuments({ tripduration: { $gt: 120 }, usertype: "Subscriber" })