MongoDB
MongoDB
Types of Databases
The two most important advantages that NoSQL databases provide are:
Flexibility
High Scalability
Relational Database:
Name Age
Joe 25
Document Database:
1
"name": "Joe",
"age": 25
JSON
Flexibility
First storing the subscription expiry date of a prime user, we can add new
keys to documents as and when required.
Non-Prime user
"name": "Joe",
"age": 25
JSON
Prime user
"name": "Sam",
"age": 25,
"expiry_date": "01-01-2022"
JSON
This makes querying simpler & faster as it doesn’t involve any join
operations.
Relational Databases:
Non-Relational Databases:
10
11
{
"user_id": 1,
"name": "Joe",
"age": 25,
"gender": "F",
"cart": [
"product_id": 10,
"quantity": 2
...
JSON
Expand
High Scalability
On a sale day like the Big Billion Day, the number of requests related to
various purchases and products increases enormously.
Ideally, we would like to have our database serve the additional load
without any issue
Scalability
2. Sharding
Advantages:
MongoDB
Use cases:
Real-time Analytics
Gaming
Internet of Things
Collection
Table vs Collection:
Choosing a Database
Submit Feedback
Setting up MongoDB
3. Connect to MongoDB
Creating Cluster
Open
mongodb login
Accept the Privacy policy and terms of Service and click on the
submit button.
The welcome page will be visible for a few seconds, wait for the
main page.
o Select
Learn MongoDB
o Select
New to MongoDB
o Select language as
JavaScript / Node.js
o Select
o Select
o Click on the
Finish
button.
Creating Cluster:
Finish
Free
button.
Security Quickstart
page.
Username
field.
Password
field.
My Local Environment
IP Address
field.
Click on the
Add Entry
button.
Click on the
button.
Now
MongoDB Cluster
is created.
2. Install MongoDB Shell
C: drive
Now click on the
Start
environmental variables
Open the
.
In the opened window, Click on the
Environment variables
button
Go to the
System variables
Path
Edit
button.
Create a new variable and add the path of the bin folder, now click
on the
New
Browse
bin
Ok
Run
mongosh --version
3. Connect to MongoDB
Connect to the Database using Shell. Get the connection string from
the website and execute it in the terminal as shown below.
Enter the user password and press the enter key.
show dbs
command.
Creating a Database
use <databaseName>
command
createCollection()
Unlike in Relational Databases, we need not specify the fields
beforehand.
CRUD Operations
Insert Document
insertOne()
function.
Syntax
db.collection.insertOne({...})
SHELL
SQL Vs MongoDB:
As MongoDB is flexible with the structure of the document, we can insert
JSON with available fields.
db.product.insertOne(
name: "Bat",
price: 2999,
brand: "Yonex",
category: "Sport"
SHELL
discount_in_percentage
information.
Unique Id:
_id
Output:
_id: ObjectId("6098fb...")
name: "Bat",
price: 2999,
brand: "Yonex",
category: "Sport"
SHELL
Read Document
find()
function
Syntax
1
db.collection.find()
SHELL
using * in SQL
product
find()
function.
Output:
10
11
{
_id: ObjectId("608a3a..."),
price: 22995,
category: "Watches",
brand: "Fossil",
discount_in_percentage: 10
},
_id: ObjectId("6098fb..."),
SHELL
Expand
insertMany()
function.
Syntax
db.collection.insertMany([{...}])
SHELL
Similar to the
WHERE
find()
.
Update Document
updateMany()
function.
Update price of
"Bat"
to 2500:
MongoDB:
1
2
db.product.updateMany(
{name: "Bat"},
SHELL
SQL
UPDATE product
SQL
Set the
discount_in_percentage
MongoDB:
db.product.updateMany(
{},
{
$set: {"discount_in_percentage": 0}
SHELL
SQL
UPDATE product
SET discount_in_percentage=0;
SQL
$set
operator in MongoDB. This operator will set the value of the field with
specified value, if the specified field is not present it will add the new field
Delete Document
We can use
deleteMany()
to delete documents.
MongoDB:
db.product.deleteMany(
condition
SHELL
SQL
1
2
WHERE condition;
SQL
If we want to delete all the documents ina collection we can use empty
brases
{}
Syntax:
Projection
We can use MongoDB projection, which can be used to extract only the
fields you need from a document.
In order to get the field in the result we need to specify the field name as
key and value as 1. Similarly if we want to exclude a field, we need to
specify the value as 0
Get the
id
name
, and
price
db.product.find(
{category: "Electronics"},
SHELL
SQL
FROM product
SQL
Excluding Fields:
Get all fields except the category of all products that belong to the
Electronics category.
MongoDB:
db.product.find(
{category: "Electronics"},
{category: 0}
)
SHELL
db.product.find(
condition,
SHELL
Projection F
Inclusion
Exclusion
Common Mistake
Example:
db.product.find(
{category: "Electronics"},
{category: 0, name: 1}
)
C
Submit Feedback
Conditional Operators
Syntax:
db.collection.find(
SHELL
Operator(SQL) Vs Operator(MongoDB):
Operator(SQL) Operator(MongoDB)
= $eq
> $gt
>= $gte
< $lt
Operator(SQL) Operator(MongoDB)
=< $lte
Examples:
SHELL
SHELL
SHELL
$eq
operator.
Query:
db.product.find(
{category: "Sport"},
SHELL
Output:
1
2
10
_id: ObjectId("608a3c..."),
name: 'Bat',
price: 2999,
brand: Yonex,
category: 'Sport'
},
...
SHELL
Ordering Results
Sort Function
sort()
MongoDB:
db.collection_name.find().sort({field_name: order})
SHELL
SQL
SELECT *
FROM table_name
SQL
SQL Vs MongoDB:
SQL MongoDB
ASC 1
DESC -1
Example:
MongoDB:
db.product.find().sort({price:-1})
SHELL
SQL
SELECT *
FROM product
SQL
Common Mistake
function.
db.product.sort(
{price:-1}
).find()
SHELL
MongoDB:
db.product.find().sort({price:-1, name:1})
SHELL
SQL
SELECT *
FROM product
ORDER BY
price DESC,
name ASC;
SQL
Pagination
We use
limit()
and
skip()
db.product.find().skip(n).limit(m)
SHELL
n = Skips
documents
m = Selects
documents
SQL Vs MongoDB:
SQL MongoDB
OFFSET skip
LIMIT limit
Example:
MongoDB:
db.product.find().sort(
{rating:-1}
).skip(5).limit(10)
SHELL
SQL
SELECT *
FROM product
LIMIT 10 OFFSET 5;
SQL
Logical Operators
AND ($and)
OR ($or)
NOT ($not)
we wrap conditions in
[]
Syntax:
db.collection.function(
SHELL
SQL Vs MongoDB:
SQL MongoDB
AND $and
OR $or
NOT $not
AND Operator
Puma and
MongoDB:
db.product.find({
$and: [
{brand: "Puma"},
{price:{$lte: 1500}}
})
SHELL
AND & OR
MongoDB:
db.product.find({
$and: [
{brand: "Denim"}]
},
{price:{$lte: 1500}}
})
SHELL
Query Walkthrough
Query:
db.product.find({
discount_in_percentage:{$gt: 10}
})
SHELL
Query:
db.product.find({
discount_in_percentage:{$gt: 10}
}).sort({price: 1})
SHELL
Query:
3
db.product.find({
discount_in_percentage:{$gt: 5}
}).sort({price: 1}).limit(10)
SHELL
Query:
db.product.find({
discount_in_percentage: {$gt: 5}
}).sort({price: 1}).skip(10).limit(5)
SHELL
Submit Feedback
Aggregate
aggregate
function.
Syntax:
db.collection.aggregate([...]);
SHELL
Group By
Group By Syntax:
Find the total score of each player.
GROUP BY
name
MongoDB:
db.playerMatchDetails.aggregate([
$group:{_id: '$name',
]);
SHELL
SQL
4
SELECT
FROM player_match_details
GROUP BY name;
SQL
Simple Aggregation
$group
stage returns a single document that aggregates values across all of all
the documents.
MongoDB:
db.players.aggregate([{
$group:{
_id: null,
}]);
SHELL
SQL
SELECT
AVG(score) as avg_score
FROM player_match_details
SQL
Other Aggregations:
Sum $sum
Avg $avg
Max $max
Min $min
Query Walkthrough
Example I:
Query:
db.playerMatchDetails.aggregate([
{
$group: {
_id: '$name',
]);
SHELL
Example II:
Query:
db.playerMatchDetails.aggregate([
$group: {
_id: null,
]);
SHELL
Example III:
Query:
db.playerMatchDetails.aggregate([
$group: {
_id: '$name',
]);
SHELL
Example:
Approach:
Stages:
db.playerMatchDetails.aggregate([
stage1,
stage2,
...
]);
SHELL
Operators:
operators
, i.e.
$group
$match
$sort
$skip
$limit
etc.
Choosing Operators
Stages Operator Description
We use
$match
Solution:
Query:
db.playerMatchDetails.aggregate([
},
$group:{_id:'$name',avgScore:{$avg:'$score'}}
]);
SHELL
Order of Stages:
The order of stages is important as the output of stages is passed as
input for the next stage.
Query:
db.playerMatchDetails.aggregate([
$group:{_id:'$name', ...}
},
]);
SHELL
Output:
[]
SHELL
As the output for the first stage doesn’t have a year field, the
second stage results in an empty list.
Aggregation pipeline
Example:
Approach:
Selecting Operators:
Aggregation:
MongoDB:
10
11
12
13
14
15
16
17
18
db.playerMatchDetails.aggregate([
$match: {
year: 2021
},
$group: {
_id: '$name',
},
$match: {
]);
SHELL
Collapse
This framework of aggregating data using a sequence of stages is called
an
aggregation pipeline
Query Walkthrough
Example I :
$group:{
SHELL
Solution:
1
db.playerMatchDetails.aggregate([
$group: {
_id: '$name',
]);
SHELL
Example II :
Get the total number of sixes scored by each player in the year
2021 and also consider only T20 matches.
$match: {
SHELL
$group:{
_id: '$name',
SHELL
Solution:
10
11
12
13
14
db.playerMatchDetails.aggregate([
$match: {
}
},
$group: {
_id: '$name',
]);
SHELL
Collapse
Example III :
$match: {
SHELL
$group:{
_id: '$name',
totalSixes: {$sum: '$sixes'}
SHELL
SHELL
Solution:
10
11
12
13
14
15
db.playerMatchDetails.aggregate([
},
$group: {
_id: '$name',
},
]);
SHELL
Collapse
Submit Feedback
Nested Document
Product
and
User
Collection documents.
Product Collection :
name:"Smart watch",
price:22995,
category:"Watches",
brand:"Fossil",
discountInPercentage: 10
SHELL
User Collection :
name: "Joe",
age: 25,
gender: "M"
SHELL
Let’s update the Rating feature in our e-commerce app. Our database
should store the following details for each product:
Average Ratings
In Document Database:
Nested Documents are also called
Embedded Documents
CRUD Operations
Basic CRUD operations are very much similar to handling any document.
insertOne
updateMany
insertMany
deleteMany
Example:
Query:
5
6
db.product.find({
ratings: {
avgRating: 3.5,
noOfUsersRated: 1000
})
SHELL
Common Mistakes
Note: Order of the fields should be same as the one mentioned in the
document.
Dot(.) notation
Example I :
db.product.find(
SHELL
Example II :
db.product.find(
"ratings.avgRating": 3.5,
SHELL
Dot(.)
Query Walkthrough
Example I :
Query:
db.product.find(
SHELL
Example II :
Find all best rated(i.e average rating >= 4) products which belongs
to
o Mobiles category
Query:
db.product.find({
$and: [
{category: "Mobiles"},
})
SHELL
Example III :
Find all best rated(i.e average rating >= 4) products which belongs
to
Query:
db.product.find({
$and: [
{category: "Mobiles"},
}).sort({"ratings.avgRating": -1})
SHELL
Array Field
User Collection :
name: {
first: "Joe",
last: "Jonas"
age: 25,
gender: "M"
SHELL
User
Product
In the Relational Database need to create a new table wish list with
user_id
and
product_id
user
documents itself.
MongoDB
{
name: {
first: "Joe",
last: "Jonas"
},
age: 25,
gender: "M",
SHELL
name id
Query Walkthrough
Example I :
Users with one plus 9R(id is 102) as one of the wish list.
Query
db.user.find({wishList:102})
SHELL
Example II :
Users with one plus 9R(id is 102) and case cover(id is 103) in their
wish list.
Query
4
5
db.user.find({
wishList:{
})
SHELL
Note: The
$all
operator selects the documents where the value of a field is an array that
contains all the specified elements.
Example III :
Users with one plus 9R(id is 102) or case cover(id is 103) in their
wish list.
Query
db.user.find({
wishList:{
})
SHELL
choose the
$in
$or
operator when performing equality checks on the same field.
The
$in
operator selects the documents where the value of a field equals any
value in the specified array.
Example IV :
Query
db.user.find({wishList:{$size: 0}})
SHELL
The
$size
operator matches any array with the number of elements specified by the
argument.
Multiple Conditions
o Products
o Quantity
User Collection:
8
9
10
...
purchaseHistory: [
productId: 201,
quantity: 1,
...
SHELL
Example I:
MongoDB
db.user.find(
"purchaseHistory.quantity": {$gt: 1}
}
)
SHELL
But, here we get users whose products in purchase history match any one
of the conditions.
Element Match
The
$elemMatch
operator matches documents that contain an array field with at least one
array element that satisfies all the specified criteria.
MongoDB
db.user.find({
"purchaseHistory": {
$elemMatch: {
productId: 201,
quantity: {$gt: 1}
})
SHELL
Query Walkthrough
Example I :
Query
db.user.find(
{"purchaseHistory.productId": 249}
SHELL
Example II :
o Sanitiser(id is 266)
Query
db.user.find(
SHELL
Example III :
Query
db.user.find({
"purchaseHistory": {
$elemMatch: {
productId: 249,
}
})
SHELL
Submit Feedback
Table of Content
Unlike SQL databases that use rows and columns, MongoDB stores data
as JSON-like documents, making it easier to handle unstructured data and
providing greater flexibility in terms of schema design.
For example:
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "Alice",
"age": 25,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
},
"hobbies": ["reading", "cycling"]
}
To create a new database and collection in MongoDB, you can use the
mongo shell:
use mydatabase
db.createCollection("mycollection")
CRUD operations in MongoDB are used to create, read, update, and delete
documents.
This query retrieves all documents from the collection where the age field
is greater than or equal to 20.
db.collection.createIndex({ name: 1 })
To perform data import and export in MongoDB, you can use the
mongoimport and mongoexport tools. These tools allow you to import
data from JSON, CSV or TSV files into MongoDB and export data from
MongoDB collections to JSON or CSV files.
Import Data:
15. What are MongoDB Aggregation Pipelines and How are They
Used?
Each stage performs an operation on the input documents and passes the
results to the next stage.
db.orders.aggregate([
{ $match: { status: "A" } }, // Stage 1: Filter documents by status
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }, // Stage 2:
Group by customer ID and sum the amount
{ $sort: { total: -1 } } // Stage 3: Sort by total in descending
order
])
In this example:
db.sales.aggregate([
{ $match: { status: "completed" } }, // Filter completed sales
{ $group: { _id: "$product", totalSales: { $sum: "$amount" } } }, //
Group by product and sum the sales amount
{ $sort: { totalSales: -1 } } // Sort by total sales in descending order
])
19. What are TTL Indexes, and How are They Used in MongoDB?
To create a TTL index, you can specify the expiration time in seconds:
This index will remove documents from the sessions collection 1 hour
(3600 seconds) after the createdAt field's value.
20. How to Handle Schema Design and Data Modeling in
MongoDB?
GridFS splits a large file into smaller chunks and stores each chunk as a
separate document within two collections: fs.files and fs.chunks. This
allows for efficient storage and retrieval of large files, such as images,
videos, or large datasets.
1. WiredTiger:
2. MMAPv1:
Example in JavaScript:
session.startTransaction();
try {
db.collection1.insertOne({ name: "Alice" }, { session });
db.collection2.insertOne({ name: "Bob" }, { session });
session.commitTransaction();
} catch (error) {
session.abortTransaction();
} finally {
session.endSession();
}
25. What is MongoDB Atlas, and How Does it Differ From Self-
Hosted MongoDB?
27. What are Capped Collections, and When are They Useful?
Replica Sets: Use replica sets to ensure data redundancy and high
availability. Regularly test the failover and recovery process.
30. Describe the Process of Upgrading MongoDB to a Newer
Version
31. What are Change Streams in MongoDB, and How are They
Used?
To use Change Streams, you typically open a change stream cursor and
process the change events as they occur.
Example:
This example listens for changes in the orders collection and logs the
change events.
Example:
Index Hinting: Use index hints to force the query optimizer to use
a specific index.
Example:
db.collection.mapReduce(
function() { emit(this.category, this.price); },
function(key, values) { return Array.sum(values); },
{ out: "category_totals" }
);
This example calculates the total price for each category in a collection.
Example:
In this example, a text index is created on the content field, and a text
search query is performed to find documents containing the word
"mongodb."
These questions typically include tasks like retrieving specific data using
filters, sorting and paginating results, and using projection to select fields.
"[
{
""_id"": 1,
""name"": ""John Doe"",
""age"": 28,
""position"": ""Software Engineer"",
""salary"": 80000,
""department"": ""Engineering"",
""hire_date"": ISODate(""2021-01-15"")
},
{
""_id"": 2,
""name"": ""Jane Smith"",
""age"": 34,
""position"": ""Project Manager"",
""salary"": 95000,
""department"": ""Engineering"",
""hire_date"": ISODate(""2019-06-23"")
},
{
""_id"": 3,
""name"": ""Emily Johnson"",
""age"": 41,
""position"": ""CTO"",
""salary"": 150000,
""department"": ""Management"",
""hire_date"": ISODate(""2015-03-12"")
},
{
""_id"": 4,
""name"": ""Michael Brown"",
""age"": 29,
""position"": ""Software Engineer"",
""salary"": 85000,
""department"": ""Engineering"",
""hire_date"": ISODate(""2020-07-30"")
},
{
""_id"": 5,
""name"": ""Sarah Davis"",
""age"": 26,
""position"": ""UI/UX Designer"",
""salary"": 70000,
""department"": ""Design"",
""hire_date"": ISODate(""2022-10-12"")
}
]"
Query:
Output:
[
{
"_id": 1,
"name": "John Doe",
"age": 28,
"position": "Software Engineer",
"salary": 80000,
"department": "Engineering",
"hire_date": ISODate("2021-01-15")
},
{
"_id": 2,
"name": "Jane Smith",
"age": 34,
"position": "Project Manager",
"salary": 95000,
"department": "Engineering",
"hire_date": ISODate("2019-06-23")
},
{
"_id": 4,
"name": "Michael Brown",
"age": 29,
"position": "Software Engineer",
"salary": 85000,
"department": "Engineering",
"hire_date": ISODate("2020-07-30")
}
]
Explanation:
Query:
Output:
[
{
"_id": 3,
"name": "Emily Johnson",
"age": 41,
"position": "CTO",
"salary": 150000,
"department": "Management",
"hire_date": ISODate("2015-03-12")
}
]
Explanation:
This query sorts all employees by salary in descending order and retrieves
the top document, which is the employee with the highest salary.
Query:
Output:
Explanation:
This query updates the salary of the employee named "John Doe" to
90000.
Query:
db.employees.aggregate([
{ $group: { _id: "$department", count: { $sum: 1 } } }
])
Output:
[
{ "_id": "Engineering", "count": 3 },
{ "_id": "Management", "count": 1 },
{ "_id": "Design", "count": 1 }
]
Explanation:
This query groups the employees by the department field and counts the
number of employees in each department.
Query:
Output:
Explanation:
This query adds a new field bonus with a value of 5000 to all employees in
the "Engineering" department.
Query:
db.employees.aggregate([
{ $addFields: { nameLength: { $strLenCP: "$name" } } },
{ $sort: { nameLength: -1 } },
{ $project: { nameLength: 0 } }
])
Output:
[
{
"_id": 2,
"name": "Jane Smith",
"age": 34,
"position": "Project Manager",
"salary": 95000,
"department": "Engineering",
"hire_date": ISODate("2019-06-23")
},
{
"_id": 3,
"name": "Emily Johnson",
"age": 41,
"position": "CTO",
"salary": 150000,
"department": "Management",
"hire_date": ISODate("2015-03-12")
},
{
"_id": 1,
"name": "John Doe",
"age": 28,
"position": "Software Engineer",
"salary": 80000,
"department": "Engineering",
"hire_date": ISODate("2021-01-15")
},
{
"_id": 4,
"name": "Michael Brown",
"age": 29,
"position": "Software Engineer",
"salary": 85000,
"department": "Engineering",
"hire_date": ISODate("2020-07-30")
},
{
"_id": 5,
"name": "Sarah Davis",
"age": 26,
"position": "UI/UX Designer",
"salary": 70000,
"department": "Design",
"hire_date": ISODate("2022-10-12")
}
]
Explanation:
This query calculates the length of each employee's name, sorts the
documents by this length in descending order, and removes the
temporary nameLength field from the output.
Query:
db.employees.aggregate([
{ $match: { department: "Engineering" } },
{ $group: { _id: null, averageSalary: { $avg: "$salary" } } }
])
Output:
[
{ "_id": null, "averageSalary": 86666.66666666667 }
]
Explanation:
Query:
db.employees.aggregate([
{ $group: { _id: "$department", averageSalary: { $avg: "$salary" } } },
{ $sort: { averageSalary: -1 } },
{ $limit: 1 }
])
Output:
[
{ "_id": "Management", "averageSalary": 150000 }
]
Explanation:
This query groups employees by department, calculates the average
salary for each department, sorts these averages in descending order, and
retrieves the department with the highest average salary.
Query:
db.employees.aggregate([
{ $group: { _id: { $year: "$hire_date" }, totalHired: { $sum: 1 } } }
])
Output:
[
{ "_id": 2015, "totalHired": 1 },
{ "_id": 2019, "totalHired": 1 },
{ "_id": 2020, "totalHired": 1 },
{ "_id": 2021, "totalHired": 1 },
{ "_id": 2022, "totalHired": 1 }
]
Explanation:
This query groups employees by the year they were hired, which is
extracted from the hire_date field, and counts the total number of
employees hired each year.
Query:
db.employees.aggregate([
{ $match: { department: "Engineering" } },
{
$group: {
_id: null,
highestSalary: { $max: "$salary" },
lowestSalary: { $min: "$salary" }
}
}
])
Output:
[
{ "_id": null, "highestSalary": 95000, "lowestSalary": 80000 }
]
Explanation: