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, We’ll walk through CRUD operations (Create, Read, Update, Delete) in MongoDB using a simple example: a 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: "harshitha",
age: 20,
department: "CSBS",
year: 2,
cgpa: 9
}
{
student_id: "S002",
name: "Madhumitha",
age: 21,
department: "CSBS",
year: 3,
cgpa: 8.5
}
{
student_id: "S003",
name: "Lokhitha",
age: 20,
department: "MI",
year: 3,
cgpa: 8
}
{
student_id: "S004",
name: "Harish",
age: 22,
department: "IT",
year: 3,
cgpa: 9.2
}
{
student_id: "S005",
name: "Dhaaraneesh",
age: 20,
department: "CIVIL",
year: 2,
cgpa: 8.8
}
{
student_id: "S006",
name: "Harish",
age: 22,
department: "CSE",
year: 3,
cgpa: 9.2
}
{
student_id:"S008",
name:"Ravi",
age:19,
department:"ECE",
year:1,
cgpa:7.8
}
Top comments (0)