๐ฏ Objective
To gain hands-on experience performing CRUD (Create, Read, Update, Delete) operations in MongoDB Atlas using a simple college student schema.
MongoDB is a NoSQL document database that stores data in a flexible, JSON-like format โ making it ideal for dynamic and structured data such as student records.
๐งฑ Schema Design
Weโll use a collection called students.
Each student document follows this structure:
{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}
โ๏ธStep 1: Create (Insert)
Insert at least 5 student records into the students collection.
MongoDB Query:
db.students.insertMany
{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
},
{
"student_id": "S002",
"name": "Priya",
"age": 19,
"department": "CSE",
"year": 1,
"cgpa": 8.7
},
{
"student_id": "S003",
"name": "Karthik",
"age": 21,
"department": "IT",
"year": 3,
"cgpa": 7.4
},
{
"student_id": "S004",
"name": "Meena",
"age": 22,
"department": "ECE",
"year": 4,
"cgpa": 8.1
},
{
"student_id": "S005",
"name": "Arun",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9.2
}
โ
Result in Atlas:
Inserted 5 documents successfully.
๐ Step 2: Read (Query)
a. Display all student records
db.students.find();
b. Find all students with CGPA > 8
db.students.find({ cgpa: { $gt: 8 } });
c. Find students belonging to the Computer Science department
db.students.find({ department: { $in: ["CSE", "CSBS"] } });
โ
Tip:
Use the MongoDB Atlas โFilterโ option to test these queries easily.
โ๏ธ Step 3: Update
a. Update the CGPA of a specific student
Example: Update Santhoshโs CGPA to 9.5
db.students.updateOne(
{ student_id: "S001" },
{ $set: { cgpa: 9.5 } }
);
Modified 1 document.
b. Increase the year of study for all 3rd-year students by 1
db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
);
โ
Output:
Shows how many documents were modified.
๐๏ธ Step 4: Delete
a. Delete one student record by student_id
Example: Delete student S003
db.students.deleteOne({ student_id: "S003" });
b. Delete all students having CGPA < 7.5
db.students.deleteMany({ cgpa: { $lt: 7.5 } });
โ
Output:
Displays number of deleted documents.
๐คStep 5: Export the Collection
After performing all CRUD operations, you can export your collection from MongoDB Atlas:
๐ชถ Steps:
- Go to your cluster โ Collections โ Select students.
- Click Export Collection.
- Choose JSON or CSV format.
- Download your file.
๐ธ Deliverables
โ
MongoDB Queries (as shown above)
โ
Screenshots of query results (from Atlas)
โ
Exported students.json or students.csv file
๐** Conclusion**
This hands-on lab demonstrates how easily MongoDB handles CRUD operations using a flexible schema.
The students collection can be expanded further to include additional attributes like address, email, or course list โ giving you real-world database experience for college or project work.
Top comments (0)