MongoDB
MongoDB
Topics
What is mongodb?
Installation (MAC & Windows)
Mongodb ATLAS
CRUD Operations
Operators (conditions, logical, relations)
Aggregate Framework
Data Modeling (Relationship)
Schema Validations
Indexes
Python + Mongodb
Interview questions
What is a Database?
What is a Database?
What is MongoDB?
MPRASHANT
MongoDB Structure
documents
MPRASHANT
MPRASHANT
Brief history and background.
MongoDb Features
Scalable: Easy to distribute data across multiple machines as data
grows.
SQL vs NoSQL
MPRASHANT
Scalability
Schema Flexibility
Performance
Installation
Default port of mongodb is 27017
Create
Database and Collection
use database_name
db.createCollection("myCollection")
show dbs
show collections
MPRASHANT
DROP
Database and Collection
collection_name.drop()
db.dropDatabase()
MPRASHANT
CRUD Operations
CRUD
CREATE
READ
UPDATE
DELETE
MPRASHANT
CREATE READ
insertOne() find()
insertMany() findOne()
UPDATE DELETE
updateOne() deleteOne()
updateMany() deleteMany()
MPRASHANT
maker
model
fuel type
transmission
engine
cc
torque
features
sunroof
airbags
MPRASHANT
UPDATE
updateOne()
updateMany()
MPRASHANT
MPRASHANT
MPRASHANT Updating Array
MPRASHANT
Unset Field
upsert is a combination of the operations
"update" and "insert."
Data Types
MPRASHANT
Operators
MPRASHANT
Comparison Operators
$eq $lt $gt $lte $gte $ne
= < > <= >= !=
Example: {age:{$eq:25}}
MPRASHANT
$in $nin
{age:
{$in:[20,30]}
}
MPRASHANT
Logical Operators
MPRASHANT
{$or:[
{age:{$eq:28}},
{name:"raju"}
]}
Element Operators
MPRASHANT
$exists
{age:{$exists:true}}
$type
Here we can filter the content based on BASON
type like string, bool etc
This can be useful to find field with null values
{name:{$type:"string"}}
Array Operators
MPRASHANT
$size
Return all documents that match specified array
size
db.collection.find({hobbies:{$size:4}})
$all
Return all documents that match the pattern
(all user with hobbies of play and read)
db.collection.find({hobbies:{$all:["play","read"]}})
UseCase:
Aggregate
Framework
Aggregation is a powerful framework for
Complex operations
db.collection.aggregate(
[
{stage1},
{stage2}...
], {option}
)
MPRASHANT
Most commonly used stages in MongoDB aggregation:
$match
$group
$project
$sort
$limit
$unwind
$lookup
$addFields
$count
$skip
GROUPING
No. of cars of each Brand (maker)
Hyundai Tata Toyota
Honda
Suzuki
GROUP
Syntax of ‘group’
db.collection.aggregate([
{
$group: {
_id: "$category",
totalAmount: { $sum: "$amount" },
averageAmount: { $avg: "$amount" },
minAmount: { $min: "$amount" },
maxAmount: { $max: "$amount" },
amountsList: { $push: "$amount" },
uniqueAmounts: { $addToSet: "$amount" }
}
}
])
MPRASHANT
db.cars.aggregate([
{$group:
{_id:"$maker"}
}])
MPRASHANT
db.cars.aggregate([
{ $group:
{ _id: "$maker",
TotalCars: { $sum: 1 }
}
}] )
db.cars.aggregate([
{ $group:
{ _id: "$fuel_type",
TotalCars: { $sum: 1 }
} }] )
MATCH
MPRASHANT
db.cars.aggregate(
[{$match:
{maker:"Hyundai",
"engine.cc":{$gt:1000}
}}])
COUNT
MPRASHANT
db.cars.aggregate(
[{$match:
{maker:"Hyundai"}},
{$count: "Total_cars"}
])
MPRASHANT
db.cars.aggregate(
[ {$match:{maker:"Hyundai"}},
{$group:
{_id:"$fuel_type",
Totalcars:{$sum:1}}
}
])
Project
MPRASHANT
Find all the Hyundai cars and only show Maker, Model and
Fuel_type details
db.cars.aggregate(
[{ $match: { maker: "Hyundai" }},
{ $project:
{ maker: 1, model: 1, fuel_type: 1,_id:0 }}
])
SORT
MPRASHANT
db.cars.aggregate(
[{ $match: { maker: "Hyundai" }},
{ $project:
{ maker: 1, model: 1, fuel_type: 1,_id:0 }},
{ $sort: {model:1}}
])
db.cars.aggregate(
{$sortByCount:"$maker"}
)
Unwind
MPRASHANT
db.cars.aggregate([
{ $unwind: "$owners" }
])
String Operators
$concat $toUpper $toLower $regexMatch
$ltrim $split
Document - https://www.mongodb.com/docs/manual/reference/operator/aggregation/#string-expression-operators
db.collection.aggregate([
{
$project: {
fullName: { $concat: ["$firstName", " ", "$lastName"] }
// Concatenates firstName and lastName with a space
}
}
])
MPRASHANT
List down all the Hyundai cars and print the name as
Maker + Model i.e. CarName Hyundai Creta
db.cars.aggregate(
[ { $match: { maker: "Hyundai" } },
{ $project: { _id: 0,
CarName: { $concat: ["$maker", " ",
"$model"] } } }] )
$regexMatch
db.cars.aggregate([
{ $project: { model: 1, _id: 0,
is_diesel:
{ $regexMatch:
{ input: "$fuel_type",
regex: "Die" }
} } }])
OUT
MPRASHANT
db.cars.aggregate(
[ { $match: { maker: "Hyundai" } },
{ $project: { _id: 0,
CarName: { $concat: ["$maker", " ",
"$model"] } } },
{$out:"hyundai_cars"}] )
Airthmetic Operators
$add $subtract $divide $multiply $round
$abs $ceil
Document - https://www.mongodb.com/docs/manual/reference/operator/aggregation/#arithmetic-expression-operators
db.collection.aggregate([
{
$project: {
sum: { $add: [2, 3] } // 2 + 3
}
}
])
MPRASHANT
Print all the cars model and price with hike of 55000
(similarly we can use $subtract too)
db.cars.aggregate(
{$project:
{model:1, _id:0,
price:{$add:["$price",50000]}}})
AddFields / Set
MPRASHANT
db.cars.aggregate([
{$project:{model:1,_id:0,price:1}},
{$addFields:
{price_in_lakhs:
{$divide: ["$price", 100000]}}}])
MPRASHANT
db.cars.aggregate([
{ $match: { maker: "Hyundai" } },
{ $set: { total_service_cost: { $sum: "$service_history.cost" } } },
{$project: {model:1, maker:1, _id:0, total_service_cost:1}}])
Conditional Operators
$cond $ifNull $switch
Document - https://www.mongodb.com/docs/manual/reference/operator/aggregation/#conditional-expression-operators
The $cond operator in MongoDB is a ternary
conditional operator
db.cars.aggregate([
{ $project: {
_id: 0, maker: 1, model: 1,
fuelCategory: {
$cond: {
if: { $eq: ["$fuel_type", "Petrol"] },
then: "Petrol Car",
else: "Non-Petrol Car"
}
}}}])
$switch
Suppose we want to categorize the price of the car into three categories:
"Budget", "Midrange", and "Premium"
db.cars.aggregate([
{$project: {
_id: 0, maker: 1, model: 1,
priceCategory: {
$switch: {
branches: [
{ case: { $lt: ["$price", 500000] },
then: "Budget" },
{ case: { $and: [{ $gte: ["$price", 500000] }, { $lt: ["$price", 1000000] }] },
then: "Midrange" },
{ case: { $gte: ["$price", 1000000] },
then: "Premium" }
],
default: "Unknown"
}
}}} ]);
DATE Operators
$dateAdd $dateDiff $month $year $hour
$dateOfMonth $dayOfYear
Document - https://www.mongodb.com/docs/manual/reference/operator/aggregation/#date-expression-operators
db.collection.aggregate([
{
$project: {
newDate: {
$dateAdd: {
startDate: new Date("2024-08-29"), // Starting date
unit: "day", // Unit to add (e.g., "day", "month", "year")
amount: 7 // Amount to add
}
}
}
}
])
Variables
System Generated Variables
Document: https://www.mongodb.com/docs/manual/reference/aggregation-variables/#system-variables
db.cars.aggregate(
{$project:{_id:0,model:1,date:"$$NOW"}})
User Defined Variables
Relations
MPRASHANT
User
Many : Many
Courses Students
Raju
Java
Sham
Python
SQL
Mongodb
Raju
Math
Sham
How can we maintain
relationship in MongoDB?
users orders
{
"_id": "user1",
"name": "Amit Sharma",
"email": "amit.sharma@example.com",
Embedded Documents "orders": [
{
"_id": "order1",
users orders
MPRASHANT
db.users.aggregate([
{
"$lookup": {
"from": "orders", // The target collection to join with
"localField": "_id", // The field from the 'users' collection
"foreignField": "user_id", // The field from the 'orders' collection
"as": "orders" // The name of the new array field to add to the 'users'
}
}
])
Embedded Documents
MPRASHANT
Schema Validation
MPRASHANT
Validation Levels:
strict: The document must fully comply with the schema validation
rules. If a document does not comply, it will not be inserted or
updated in the collection.
Validation Actions:
INDEXES
MPRASHANT
name: Raju
age: 25
Raju
Sham
name: Baburao
age: 45
MPRASHANT
db.collection.createIndex(
{ <field1>: <type1>,
<field2>: <type2>, ... }, {<options> })
MPRASHANT
db.cars.createIndex({ maker: 1 })
db.cars.createIndex({ model: 1 }, { unique: true })
db.cars.dropIndex(“maker”)
db.cars.getIndexes()
MPRASHANT
Types of Indexes:
Performance Considerations:
Transaction
A transaction in MongoDB is a sequence of
operations that are executed as a single unit,
ensuring that all operations either complete
successfully or are fully rolled back,
maintaining ACID properties across multiple
documents and collections.
Example of a Multi-Document Transaction:
-1000 +1000
MPRASHANT
Replications
Replication is a group of MongoDB servers
that maintain identical copies of data to
ensure high availability, redundancy, and data
durability, with one primary node handling
writes and multiple secondary nodes
replicating the data.
MPRASHANT
Sharding
Sharding is a method of distributing data
across multiple servers (shards) to enable
horizontal scaling, allowing the database to
handle large datasets and high-throughput
operations efficiently.
MPRASHANT
Interview Questions
MPRASHANT
How does MongoDB handle indexing, and what are the types of
indexes available?
What are the pros and cons of embedding documents versus using
references in MongoDB?
Embedding (Pros):
Better performance for read operations.
Simpler queries.
Embedding (Cons):
Can lead to large documents and duplication of data.
References (Pros):
More normalized data structure.
Reduces duplication.
References (Cons):
Requires additional queries (joins) which can be slower.
MPRASHANT
Answer:
updateOne(): Updates the first document that matches the query
criteria.
updateMany(): Updates all documents that match the query
criteria.
replaceOne(): Replaces the entire document with a new one,
based on the query criteria.
MPRASHANT
Answer:
Backup: Use mongodump to create a binary backup of the database.
Restore: Use mongorestore to restore the data from a backup.
Additionally, for cloud deployments, MongoDB Atlas provides
automated backups.
MPRASHANT
What are MongoDB's limitations, and how can you work around them?