Why MongoDB?
Modern applications will handle large and constantly changing data. Traditional relational databases usually need predefined schemas and structured formats. This can make it hard to be flexible when the data changes a lot.
Think about modern applications.They work with different types of data, for example:
● User profiles that may contain different fields for each user
● Product information with varying features and specifications
● Student data where some details may or may not be present
When using traditional SQL databases for such systems, developers often face challenges like:
● Defining strict table structures beforehand
● Frequently modifying schemas as requirements evolve
● Handling complex joins to connect related data makes the system harder to manage and scale efficiently. MongoDB overcomes these limitations by adopting a NoSQL, document-oriented approach,
SQL (Relational Databases)
● Data is stored in tables
● Uses rows and columns structure
● Requires a fixed schema (predefined structure)
● Example: MySQL
● In SQL:
Table: Students
| id | name | age |
MongoDB (NoSQL Database)
● Data is stored in collections
● Uses documents (JSON-like format)
● Supports a flexible schema (no strict structure)
● Example: MongoDB In MongoDB:
{
name: "Alice",
age: 20
}
COLLECTIONS AND DOCUMENTS:
Collections: A collection is like a folder where similar data is stored together in MongoDB. It is similar to a table in relational databases, but it does not have a fixed structure.
Documents: A document is a single piece of data inside that folder, stored in a flexible JSON-like format (BSON) with key-value pairs.
Example:
• Collection: Students
• Documents:
{ name: "Likitha", age: 19, course: "CSE" }
{ name: "Praacturya", age: 18, course: "ECE" }
BSON:
MongoDB uses BSON format to store the data internally, which is similar to JSON but in binary form
Why BSON?
•Faster to read and write than JSON
•It supports extra data types like date objects also more efficient for storage and retrieval
FLEXIBLE SCHEMA:
Documents in the same collection don’t need to have the same fields or structure.
Collection: Students
•Document 1:
{ name: "Likitha", age: 20, course: "CSE" }
•Document 2:
{ name: "Praacturya", course: "ECE", grade: "A" }
1.CRUD OPERATIONS
CRUD = Create, Read, Update, Delete
These are the basic operations used to interact with data.
1.CREATE(INSERT) OPERATIONS :
Used to insert the data
insert one:
insertOne(): → Adds one document
db.students.insertOne({ name: "Ravi", age: 22, course: "ECE" })
insert many:
insertMany( ): → Adds multiple documents
db.students.insertMany([ { name: "Asha", age: 20 },
{ name: "Kiran", age: 23 }
])
2. READ OPERATIONS:
Used to fetch the data
Find all:
db.students.find()
→ Returns all documents in the students collection.
Example: Shows every student record stored in the collection.
db.students.find({ age: { $gt: 21 } })
→ Returns all students whose age is greater than 21.
Find one:
db.students.findOne({ name: "Ravi" }) → Returns the first document where name is Ravi.
Example: Gets details of one student named Ravi. With condition:
3.MODIFY OPERATIONS :
Used to modify the existing data
Update one:
db.students.updateOne({ name: "Ravi" }, { $set: { age: 21 } })
→ Updates one document
Example: Changes Ravi’s age
Update many:
db.students.updateMany({ course: "CSE" },{ $set: { status: "Active" } })
→ Updates multiple documents
Example: Updates all CSE students
Replace one:
db.students.replaceOne({ name: "Ravi" }, { name: "Ravi", age: 22 })
→ Replaces entire document
Example: Rewrites Ravi’s data
4.DELETE OPERATIONS:
Delete one:
db.students.deleteOne({ name: "Ravi" })
→ Deletes one document.
Example: Removes Ravi
Delete many:
db.students.deleteMany({ age: { $lt: 18 } })
→ Deletes multiple documents
Example: Removes students below 18


Top comments (0)