Introduction to NoSQL
& MongoDB Tutorial
• Understanding the Basics and Hands-on with
MongoDB
SQL: Structured,
Fixed Schema,
Tables, ACID
SQL vs Compliance
NoSQL NoSQL: Flexible,
Scalable, Non-
relational, High
Availability
SQL vs NoSQL - Example
SELECT * FROM users
SQL: WHERE age > 25;
db.users.find({ age: { $gt:
NoSQL: 25 } })
BSON (Binary JSON): Binary format
used by MongoDB for efficiency.
JSON (JavaScript Object Notation):
Text-based format used for data
exchange.
BSON vs
Example:
JSON
JSON: { "name": "Ali", "age": 30 }
BSON: { name: "Ali", age: 30 }
(stored in binary format)
JSON
example
ACID Properties:
Atomicity: Ensures all operations in a
transaction complete successfully or
none at all.
ACID Consistency: Database remains in a
valid state before and after
Compliance transactions.
Isolation: Concurrent transactions do
not interfere with each other.
Durability: Once a transaction is
committed, it remains saved.
SQL:
• Best for structured data and relational
models.
• Suitable for applications requiring ACID
compliance.
Usage
• Used in financial, enterprise, and ERP
Differences: systems.
SQL vs NoSQL:
NoSQL • Best for unstructured or semi-structured
data.
• Scales horizontally for large datasets.
• Used in real-time analytics, big data, and
IoT applications.
• **find()** – Retrieves documents from a
collection.
• **insertOne(), insertMany()** – Adds
documents to a collection.
NoSQL
• **updateOne(), updateMany()** –
Modifies existing documents.
Functions • **deleteOne(), deleteMany()** –
Removes documents.
and Their • **sort()** – Orders query results.
Usage • **limit()** – Restricts number of
documents returned.
• **createIndex()** – Improves search
performance.
• **aggregate()** – Performs complex data
transformations and analytics.
What is MongoDB?
DOCUMENT-ORIENTED STORES DATA IN BSON HIGH AVAILABILITY
NOSQL DATABASE FORMAT AND SCALABILITY
MongoDB Installation
1 2 3
Download and Install Set
install MongoShell environment
MongoDB variables
Using MongoShell
Start MongoDB Use a database with
Run `mongosh`
service `use myDatabase`
MongoDB 01 02 03
with Install Connect to Query
MongoDB local or cloud execution
VSCode extension database inside VSCode
Creating Databases
NoSQL: Expected Result:
use testDB 'switched to db testDB'
Insert Data
NoSQL: Expected Result:
db.users.insertOne({ name: "Ali", age: { acknowledged: true, insertedId:
30 }) ObjectId('...') }
SQL vs NoSQL - Insert
SQL: NoSQL:
I NSERT INTO users (name, age) db.users.insertOne({ name: "Ali", age:
VALUES ('Ali', 30); 30 })
Data Types
String, Number, Boolean, Array, Object, Date, etc.
db.products.insertOne({ name:
Example: "Laptop", price: 1500, tags:
["electronics", "gadget"] })
{ acknowledged: true,
Expected Result: insertedId: ObjectId('...') }
Sorting and Limiting
NoSQL: Expected Result:
db.users.find().sort({ age: 1 }).limit(5) [ {name: "Aisha", age: 25}, {name: "Bilal",
age: 28} ]
• NoSQL:
Find – db.users.find({ age: { $gt: 25 } })
Documents
• Expected Result:
– [ {name: "Bilal", age: 28}, {name:
"Ali", age: 30} ]
SQL vs NoSQL - Find
SQL: NoSQL:
SELECT * FROM users WHERE age > 25; db.users.find({ age: { $gt: 25 } })
Update Documents
NoSQL:
• db.users.updateOne({ name: "Ali" },
{ $set: { age: 35 } })
Expected Result:
• { matchedCount: 1, modifiedCount: 1 }
SQL vs NoSQL - Update
SQL: NoSQL:
UPDATE users SET age = 35 WHERE name = db.users.updateOne({ name: "Ali" }, { $set:
'Ali'; { age: 35 } })
Delete Documents
NoSQL:
• db.users.deleteOne({ name: "Bilal" })
Expected Result:
• { deletedCount: 1 }
SQL vs NoSQL - Delete
DELETE FROM users WHERE
SQL: name = 'Bilal';
db.users.deleteOne({ name:
NoSQL: "Bilal" })
Comparison Operators
`$gt`, `$lt`, `$eq`, `$ne`
Example:
• db.users.find({ age: { $gt: 25 } })
Expected Result:
• [ {name: "Ali", age: 30} ]
Logical Operators
`$and`, `$or`, `$not`, `$nor`
db.users.find({ $and: [{ age:
Example: { $gt: 20 } }, { age: { $lt: 40 } }]
})
Expected Result: [ {name: "Ali", age: 30} ]
Indexing for Performance
• Improve query performance
• Create index:
– db.users.createIndex({ age: 1 })
• Expected Result:
– { "createdCollectionAutomatically": false,
"numIndexesBefore": 1, "numIndexesAfter": 2 }
Collections
• Collections = Tables in SQL
• Create:
– db.createCollection("orders")
• Expected Result:
– { "ok": 1 }
Conclusion
• MongoDB is flexible and scalable
• Useful for big data and dynamic
applications
• Efficient for high-speed data
retrieval
Q&A
• Open for questions and
discussion
Tutorial
https://www.youtube.com/watch?v=c2M-rlkkT5o&t=1641s