DEV Community

Cover image for Simple College Student Schema
Jaswant Karun
Jaswant Karun

Posted on

Simple College Student Schema

Introduction:-

MongoDB is one of the most popular NoSQL databases used by developers today for building modern, scalable applications. Unlike traditional relational databases, MongoDB stores data in flexible JSON-like documents, making it easier to work with real-world scenarios.

In this blog, I’ll walk you through CRUD operations (Create, Read, Update, Delete) in MongoDB using a simple example: a college student database. We’ll insert student details, query them, update academic information, and even delete records.

To make it more exciting, we’ll run these queries directly on MongoDB Atlas Cluster (cloud-based MongoDB). Along the way, I’ll include screenshots of my MongoDB Atlas dashboard and outputs so you can follow along visually

Outcome:-

How to insert multiple documents into a collection

How to read and filter records using queries

How to update documents (single & multiple)

How to delete documents based on conditions

How CRUD fits into real-world development

Setup: Creating a Cluster

Create a free MongoDB Atlas account

Create a cluster and a database called collegeDB

Inside it, create a collection called students

Create (Insert):-

Insert at least 5 student records into the students collection.

We cannot start by inserting 5 student records into our students collection together. We can create each student as separate document.

Code:-

{
student_id: "S001",
name: "Jaswant",
age: 20,
department: "CSBS",
year: 2,
cgpa: 9
}

{
student_id: "S002",
name: "Santhosh",
age: 21,
department: "CSE",
year: 3,
cgpa: 8.5
}

{
student_id: "S003",
name: "Gangeswara",
age: 22,
department: "ECE",
year: 4,
cgpa: 7.2
}

{
student_id: "S004",
name: "Rakshanth",
age: 19,
department: "CSBS",
year: 1,
cgpa: 9.3
}

{
student_id: "S005",
name: "Naveens",
age: 20,
department: "Mechanical",
year: 2,
cgpa: 6.8
}

Read (Query):-

Display all student records.

Fetch all students: {}

Find all students with CGPA > 8.

db.students.find({ cgpa: { $gt: 8 } })

Find students belonging to the Computer Science department.

db.students.find({ department: "CSE" })

Update:-

Update the CGPA of a specific student.

{ "student_id": "S005" }

Increase the year of study for all 3rd year students by 1.

{ "year": 3 }

Delete:-

Delete one student record by student_id.

{ "student_id": "S004" }

Result:

Delete all students having CGPA < 7.5

{ "cgpa": { "$lt": 7.5 } }

Result:

Conclusion:-

In this blog, we explored how to perform CRUD operations in MongoDB using a real-world example of a student database. Starting from inserting records, querying based on conditions, updating multiple documents, and finally deleting specific records – we’ve covered the foundation of working with MongoDB.

CRUD operations form the building blocks of every application, whether you’re managing users in a website, products in an e-commerce app, or students in a college system.

This step-by-step approach gave me a solid understanding of how a database schema works in real-world academic systems

Thanks to @santhoshnc sir for guiding and motivating us.

Top comments (0)