Introduction
As a college student, understanding database management is a fundamental skill. MongoDB, a popular NoSQL database, offers a flexible and scalable way to store data. In this guide, we’ll walk through the essential CRUD (Create, Read, Update, Delete) operations using a simple students collection schema. This is perfect for anyone getting started with MongoDB.
Schema (Collection: students)
Each document in our students collection will have the following structure:
JSON
{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}
- Create (Insert) Operations The first step is to populate our database with data. We will insert five student records into the students collection.
Query to Insert 5 Student Records:
JavaScript
db.students.insertMany([
{ "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
{ "student_id": "S002", "name": "Priya", "age": 21, "department": "IT", "year": 3, "cgpa": 8.5 },
{ "student_id": "S003", "name": "Arjun", "age": 19, "department": "CSBS", "year": 1, "cgpa": 7.2 },
{ "student_id": "S004", "name": "Deepa", "age": 22, "department": "ECE", "year": 4, "cgpa": 9.1 },
{ "student_id": "S005", "name": "Rahul", "age": 20, "department": "IT", "year": 2, "cgpa": 7.8 }
])
- Read (Query) Operations Querying is the most frequent operation. The find() method allows us to retrieve data based on specific criteria.
Query to Display all Student Records:
JavaScript
db.students.find({})
Since you have successfully performed the Read operations and have the corresponding screenshots, I will provide you with the blog post, incorporating your completed work. I will also provide the queries for the Update and Delete sections so you can complete the assignment.
CRUD Operations in MongoDB: A Beginner's Guide for College Students
Introduction
As a college student, understanding database management is a fundamental skill. MongoDB, a popular NoSQL database, offers a flexible and scalable way to store data. In this guide, we’ll walk through the essential CRUD (Create, Read, Update, Delete) operations using a simple students collection schema. This is perfect for anyone getting started with MongoDB.
Schema (Collection: students)
Each document in our students collection will have the following structure:
JSON
{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}
- Create (Insert) Operations The first step is to populate our database with data. We will insert five student records into the students collection.
Query to Insert 5 Student Records:
JavaScript
db.students.insertMany([
{ "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
{ "student_id": "S002", "name": "Priya", "age": 21, "department": "IT", "year": 3, "cgpa": 8.5 },
{ "student_id": "S003", "name": "Arjun", "age": 19, "department": "CSBS", "year": 1, "cgpa": 7.2 },
{ "student_id": "S004", "name": "Deepa", "age": 22, "department": "ECE", "year": 4, "cgpa": 9.1 },
{ "student_id": "S005", "name": "Rahul", "age": 20, "department": "IT", "year": 2, "cgpa": 7.8 }
])
Execution Result: ([Screenshot of the query result after inserting all 5 documents])
- Read (Query) Operations Querying is the most frequent operation. The find() method allows us to retrieve data based on specific criteria.
Query to Display all Student Records:
JavaScript
db.students.find({})
Execution Result:
([Screenshot of the query results showing all 5 documents, as in your screenshot "Screenshot 2025-10-08 171200.png" or similar])
Query to Find all Students with CGPA > 8:
JavaScript
db.students.find({ "cgpa": { "$gt": 8 } })
Query to Find Students from the Computer Science Department:
JavaScript
db.students.find({ "department": "CSBS" })
- Update Operations Updating allows us to modify existing documents. We will use updateOne() and updateMany() for targeted and bulk updates, respectively.
Query to Update a Specific Student's CGPA:
JavaScript
db.students.updateOne(
{ "student_id": "S001" },
{ "$set": { "cgpa": 9.2 } }
)
Query to Increase the Year of Study for all 3rd-Year Students:
JavaScript
db.students.updateMany(
{ "year": 3 },
{ "$inc": { "year": 1 } }
)
- Delete Operations Finally, we perform deletion to remove documents from the collection.
Query to Delete One Student Record by student_id:
JavaScript
db.students.deleteOne({ "student_id": "S003" })
Query to Delete all Students having CGPA < 7.5:
JavaScript
db.students.deleteMany({ "cgpa": { "$lt": 7.5 } })
Conclusion
Mastering these CRUD operations is crucial for anyone working with databases. MongoDB's intuitive, JSON-like syntax makes these operations straightforward and powerful. This exercise provides a solid foundation for more complex database tasks, helping you build robust applications in the future.
Top comments (0)