DEV Community

Cover image for CRUD Operations in MongoDB Atlas – A Beginner’s Guide with Student Database Example
NatpuEnean VA
NatpuEnean VA

Posted on

CRUD Operations in MongoDB Atlas – A Beginner’s Guide with Student Database Example

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 with filters

Update academic information

Delete records when needed

To make it more exciting, we’ll run these queries directly on MongoDB Atlas Cluster (cloud-based MongoDB). You can also include screenshots of the MongoDB Atlas dashboard and outputs so readers can follow visually.

Outcome

By the end of this blog, you’ll learn:

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 → MongoDB Atlas

Create a cluster (choose the free tier).

Inside the cluster, create a database called collegeDB.

Inside collegeDB, create a collection called students.

Create (Insert)

We’ll start by inserting student records into our students collection.

Each student is stored as a separate document:

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

{
"student_id": "S002",
"name": "Natpu",
"age": 20,
"department": "CSBS",
"year": 2,
"cgpa": 9
}

Insert multiple documents:

db.students.insertMany([
{
student_id: "S001",
name: "Kavin",
age: 20,
department: "CSBS",
year: 2,
cgpa: 9
},
{
student_id: "S002",
name: "Natpu",
age: 20,
department: "CSBS",
year: 2,
cgpa: 9
}
])

Read (Query)

Fetch all students:

db.students.find({})

Find all students with CGPA > 8:

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

Find students belonging to Computer Science department:

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

Update

Update CGPA of a specific student:

db.students.updateOne(
{ student_id: "S005" },
{ $set: { cgpa: 9.2 } }
)

Increase year of study for all 3rd-year students:

db.students.updateMany(
{ year: 3 },
{ $inc: { year: 1 } }
)

Delete

Delete one student record by ID:

db.students.deleteOne({ student_id: "S004" })

Delete all students having CGPA < 7.5:

db.students.deleteMany({ cgpa: { $lt: 7.5 } })

Troubleshooting: JSON Error in Node.js

If you’re connecting MongoDB with a Node.js backend, you might run into this error:

Failed to execute 'json' on 'Response': Unexpected end of JSON input

app.post("/students", async (req, res) => {
try {
const result = await db.collection("students").insertOne(req.body);
res.json({ success: true, id: result.insertedId });
} catch (err) {
res.status(500).json({ error: err.message });
}
});

On the frontend, check if the response has content before parsing:

const response = await fetch("/students");
let data = {};

try {
data = await response.json();
} catch (e) {
console.warn("Empty or invalid JSON response");
}

Conclusion

In this blog, we explored how to perform CRUD operations in MongoDB using a real-world example of a student database.

We:

Inserted multiple records

Queried documents with conditions

Updated both single and multiple entries

Deleted documents selectively

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 not only gave us hands-on practice with MongoDB but also demonstrated how database schemas fit into real-world academic systems.

Thanks to @santhoshnc sir for guiding and motivating us.

Top comments (0)