In this project, we will design a student management database using MongoDB. We will learn how to create student records, retrieve information, update student details, and delete records using MongoDB Shell.
This database can store information such as:
MongoDB stores records as BSON documents. Each student
will be represented by a document inside the
students collection.
Database name: school
Collection name: students
Open MongoDB Shell:
mongosh
Switch to the school database:
use school
MongoDB selects the database. It is created on disk when data is first stored in it.
Create the students collection explicitly:
db.createCollection("students")
If the collection already exists, MongoDB will report an error. In that case, you can continue using it.
Example student document:
{
student_id: 1001,
name: "Rahul Kumar",
age: 20,
gender: "Male",
course: "ADCA",
mobile: "9876543210",
email: "rahul@example.com",
address: {
city: "Aurangabad",
state: "Bihar"
},
admission_date: ISODate("2026-04-10T00:00:00Z"),
marks: {
computer: 85,
english: 78,
mathematics: 90
}
}
This document contains basic student information, an embedded address document, and subject-wise marks.
The field student_id is an application-level
identifier. MongoDB also creates an automatic
_id field for every inserted document
unless you provide one yourself.
Use insertOne() to insert one student:
db.students.insertOne({
student_id: 1001,
name: "Rahul Kumar",
age: 20,
gender: "Male",
course: "ADCA",
mobile: "9876543210",
email: "rahul@example.com",
address: {
city: "Aurangabad",
state: "Bihar"
},
admission_date: ISODate("2026-04-10T00:00:00Z"),
marks: {
computer: 85,
english: 78,
mathematics: 90
}
})
MongoDB returns an acknowledgment and the inserted
document's _id.
Run this insertion only once for the example, or use a different student ID for additional records.
Use insertMany() to add several student
documents in one operation.
db.students.insertMany([
{
student_id: 1002,
name: "Priya Singh",
age: 19,
gender: "Female",
course: "Web Development",
mobile: "9876543211",
city: "Patna",
marks: {
computer: 92,
english: 88
}
},
{
student_id: 1003,
name: "Amit Kumar",
age: 21,
gender: "Male",
course: "Tally Prime",
mobile: "9876543212",
city: "Gaya",
marks: {
computer: 75,
english: 80
}
},
{
student_id: 1004,
name: "Neha Kumari",
age: 20,
gender: "Female",
course: "ADCA",
mobile: "9876543213",
city: "Aurangabad",
marks: {
computer: 89,
english: 91
}
}
])
This example assumes these student IDs are not already present. Avoid rerunning the insertion unchanged if you want to prevent duplicate sample records.
Retrieve every student document:
db.students.find()
Display records in a readable format:
db.students.find().pretty()
Display only selected fields:
db.students.find(
{},
{
_id: 0,
student_id: 1,
name: 1,
course: 1
}
)
The projection includes the student ID, name, and
course while excluding the automatic MongoDB
_id field.
Find a student using the application-level ID:
db.students.findOne({
student_id: 1001
})
Find students enrolled in ADCA:
db.students.find({
course: "ADCA"
})
Find students older than 19:
db.students.find({
age: { $gt: 19 }
})
Find students from Aurangabad:
db.students.find({
"address.city": "Aurangabad"
})
The dot notation allows you to query a field inside
an embedded document, such as
address.city.
Use updateOne() with
$set to change specific fields.
db.students.updateOne(
{ student_id: 1001 },
{
$set: {
age: 21,
course: "Full Stack Development"
}
}
)
Update the student's city:
db.students.updateOne(
{ student_id: 1001 },
{
$set: {
"address.city": "Patna"
}
}
)
MongoDB changes only the specified fields. Other fields in the document remain unchanged.
Update the computer marks for a student:
db.students.updateOne(
{ student_id: 1001 },
{
$set: {
"marks.computer": 95
}
}
)
Display the updated record:
db.students.findOne({
student_id: 1001
})
Dot notation updates the nested
computer field inside the
marks document.
Use deleteOne() to delete one student
matching a filter.
db.students.deleteOne({
student_id: 1004
})
MongoDB returns a result showing whether a document was deleted.
Warning: Deleting a document removes it from the collection. Check the filter carefully before executing deletion commands.
To count the remaining student documents, use:
db.students.countDocuments({})
Count all student records:
db.students.countDocuments({})
Count students enrolled in ADCA:
db.students.countDocuments({
course: "ADCA"
})
Sort students by name in ascending order:
db.students.find().sort({
name: 1
})
Display the five oldest students:
db.students.find()
.sort({ age: -1 })
.limit(5)
A sort value of 1 means ascending order,
while -1 means descending order.
If every student must have a unique
student_id, create a unique index.
db.students.createIndex(
{ student_id: 1 },
{ unique: true }
)
This prevents MongoDB from accepting two documents with the same indexed student ID.
If duplicate IDs already exist, index creation can fail. Check and resolve duplicates before creating the index.
A unique index is useful when student IDs are assigned
by the application rather than relying solely on
MongoDB's automatically generated _id.
Find all students enrolled in ADCA:
db.students.find({
course: "ADCA"
})
Find students whose computer marks are at least 80:
db.students.find({
"marks.computer": { $gte: 80 }
})
Count students by course using aggregation:
db.students.aggregate([
{
$group: {
_id: "$course",
totalStudents: { $sum: 1 }
}
},
{
$sort: {
totalStudents: -1
}
}
])
The aggregation groups documents by course and counts the number of students in each group.
school database and students collection.find().student_id.1. Which method inserts one student document?
2. Which operator updates selected fields?
3. Which option enforces unique student IDs?