Simple College Student Schema in MongoDB
Introduction
MongoDB is a popular NoSQL database that stores data in flexible JSON-like documents, making it easy to work with real-world data. Unlike traditional relational databases, MongoDB doesn’t require fixed table schemas, which is perfect for modern applications.
In this blog, we’ll explore CRUD operations (Create, Read, Update, Delete) using a college student database. You’ll learn how to insert student records, query them, update information, and delete records efficiently.
All examples are demonstrated on a MongoDB Atlas Cluster, so you can follow along with screenshots and see results live.
What You’ll Learn:
How to insert multiple student records into a collection
How to query records with conditions
How to update single and multiple documents
How to delete records selectively
How CRUD applies in real-world projects
Step 1: Setup MongoDB Cluster
Sign up for a free MongoDB Atlas account.
Create a cluster and a database called collegeDB.
Inside the database, create a collection named students.
Step 2: Insert Student Records
We’ll add 5 student records using insertMany:
db.students.insertMany([
{ "student_id": "S001", "name": "Santhosh", "age": 20, "department": "CSBS", "year": 2, "cgpa": 9 },
{ "student_id": "S002", "name": "Aishwarya", "age": 21, "department": "CSE", "year": 3, "cgpa": 8.5 },
{ "student_id": "S003", "name": "Rahul", "age": 22, "department": "IT", "year": 3, "cgpa": 7.8 },
{ "student_id": "S004", "name": "Priya", "age": 20, "department": "CSBS", "year": 1, "cgpa": 9.2 },
{ "student_id": "S005", "name": "Karthik", "age": 23, "department": "CSE", "year": 4, "cgpa": 6.9 }
])
Step 3: Read (Query) Data
Display all student records:
db.students.find()
Find students with CGPA > 8:
db.students.find({ cgpa: { $gt: 8 } })
Find students in the Computer Science department:
db.students.find({ department: "CSE" })
Step 4: Update Records
Update the CGPA of a specific student (S005):
db.students.updateOne(
{ student_id: "S005" },
{ $set: { cgpa: 7.5 } }
)
Increase the year of all 3rd-year students by 1:
db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
)
Step 5: Delete Records
Delete a student by student_id:
db.students.deleteOne({ student_id: "S005" }
Conclusion
In this tutorial, we’ve covered CRUD operations in MongoDB using a student database example.
From inserting records, querying, updating, to deleting data, these operations form the foundation of almost every modern application.
This hands-on approach helps you understand NoSQL database workflows and prepares you for real-world projects like user management systems, e-commerce apps, or academic databases.
Top comments (0)