π― Objective
The goal of this lab is to gain hands-on experience with performing basic CRUD (Create, Read, Update, Delete) operations in MongoDB Atlas using a simple college student schema.
Weβll use a students collection with fields like student_id, name, age, department, year, and cgpa.
π§± Schema Structure
Each document in the students collection follows this structure:
{
"student_id": "S001",
"name": "Santhosh",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}
βοΈ Step 1: Connect to MongoDB Atlas
πͺ Step 2: Create the Database and Collection
use college;
db.createCollection("students");
β This creates a new database named college and a collection named students.
πΈ Screenshot: Collection created in Atlas.
π§© Step 3: CREATE β Insert Student Records
Weβll insert at least 5 documents into the collection.
db.students.insertMany([
{ "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
{ "student_id": "S002", "name": "Meena", "age": 19, "department": "CSE", "year": 1, "cgpa": 8.5 },
{ "student_id": "S003", "name": "Vishal", "age": 21, "department": "ECE", "year": 3, "cgpa": 7.2 },
{ "student_id": "S004", "name": "Priya", "age": 22, "department": "CSBS", "year": 3, "cgpa": 9.1 },
{ "student_id": "S005", "name": "Ravi", "age": 20, "department": "MECH", "year": 2, "cgpa": 6.8 }
]);
β Inserted 5 student documents successfully.
π Step 4: READ β Query Student Records
π 4.1 Display All Student Records
db.students.find();
π 4.2 Find Students with CGPA > 8
db.students.find({ cgpa: { $gt: 8 } });
β Displays all high-performing students.
π 4.3 Find Students from Computer Science Department
db.students.find({ department: "CSE" });
or for CSBS:
db.students.find({ department: "CSBS" });
π Description:
Filters documents where department equals CSE or CSBS.
βοΈ Step 5: UPDATE β Modify Data
π§Ύ 5.1 Update CGPA of a Specific Student
db.students.updateOne(
{ student_id: "S002" },
{ $set: { cgpa: 8.9 } }
);
β Updates Meenaβs CGPA to 8.9.
π 5.2 Increase Year of Study for All 3rd-Year Students by 1
db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
);
β Increments the year field for all 3rd-year students.
β Step 6: DELETE β Remove Data
ποΈ 6.1 Delete One Student by student_id
db.students.deleteOne({ student_id: "S005" });
β Removes the student named Ravi (S005).
π« 6.2 Delete All Students Having CGPA < 7.5
db.students.deleteMany({ cgpa: { $lt: 7.5 } });
β Deletes all underperforming students.
Top comments (0)