DEV Community

Cover image for MongoDB
Kamwemba Tinashe C
Kamwemba Tinashe C

Posted on

MongoDB

Schema (Collection: students)

[ { "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "cgpa": 9.0, "year": 2 }, { "student_id": "S002", "name": "Ravi", "age": 21, "department": "CS", "cgpa": 8.5, "year": 3 }, { "student_id": "S003", "name": "Priya", "age": 19, "department": "ECE", "cgpa": 7.8, "year": 1 }, { "student_id": "S004", "name": "Kiran", "age": 22, "department": "CS", "cgpa": 6.9, "year": 4 }, { "student_id": "S005", "name": "Anita", "age": 20, "department": "MECH", "cgpa": 7.2, "year": 2 } ] 
Enter fullscreen mode Exit fullscreen mode

** Read (Query)**

read query

All Students with CGPA > 8

{ cgpa: { $gt: 8 } } 
Enter fullscreen mode Exit fullscreen mode

cgpa > 8

Students Belonging to the Computer Science Department:

{ department: "CS" } 
Enter fullscreen mode Exit fullscreen mode

from cs department

Update

  • Update the CGPA of a specific student.
  • Increase the year of study for all 3rd year students by 1
/ Task 3️ — Update const { MongoClient } = require("mongodb"); // connection string const uri = "mongodb+srv://William:4dwQ454VgRgDtNSO@assignment.jfmqse7.mongodb.net/?retryWrites=true&w=majority&appName=Assignment"; async function run() { const client = new MongoClient(uri); try { await client.connect(); console.log("Connected to MongoDB Atlas"); const db = client.db("Assign6"); const students = db.collection("students"); // 1️ Update CGPA of a specific student (e.g., student_id = S002) const updateCGPAResult = await students.updateOne( { student_id: "S002" }, // filter { $set: { cgpa: 9.0 } } // new CGPA ); console.log(`Updated CGPA for S002 — modifiedCount: ${updateCGPAResult.modifiedCount}`); // 2️ Increase year of all 3rd-year students by 1 const increaseYearResult = await students.updateMany( { year: 3 }, // filter { $inc: { year: 1 } } // increment year by 1 ); console.log(`Increased year for ${increaseYearResult.modifiedCount} student(s)`); // Display updated collection const updatedStudents = await students.find().toArray(); console.log("\nUpdated Students Collection:"); console.table(updatedStudents.map(s => ({ student_id: s.student_id, name: s.name, year: s.year, cgpa: s.cgpa }))); } catch (err) { console.error("Error:", err); } finally { await client.close(); console.log("Connection closed."); } } run(); 
Enter fullscreen mode Exit fullscreen mode

Delete

  • Delete one student record by student_id.
  • Delete all students having CGPA < 7.5.
const { MongoClient } = require("mongodb"); const uri = "mongodb+srv://William:4dwQ454VgRgDtNSO@assignment.jfmqse7.mongodb.net/?retryWrites=true&w=majority&appName=Assignment"; async function run() { const client = new MongoClient(uri); try { await client.connect(); console.log("Connected to MongoDB Atlas"); const db = client.db("Assign6"); const students = db.collection("students"); // Delete one student record by student_id (example: S004) const deleteOneResult = await students.deleteOne({ student_id: "S004" }); console.log("Deleted student S004 — deletedCount:", deleteOneResult.deletedCount); // Delete all students having CGPA < 7.5 const deleteManyResult = await students.deleteMany({ cgpa: { $lt: 7.5 } }); console.log("Deleted students with CGPA < 7.5 — deletedCount:", deleteManyResult.deletedCount); // Display remaining collection const remainingStudents = await students.find().toArray(); console.log("Remaining Students Collection:"); console.table(remainingStudents.map(s => ({ student_id: s.student_id, name: s.name, year: s.year, cgpa: s.cgpa }))); } catch (err) { console.error("Error:", err); } finally { await client.close(); console.log("Connection closed."); } } run(); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)