In this lesson, you will practise MongoDB commands through practical assignments. These exercises cover database creation, CRUD operations, query operators, arrays, indexes, aggregation, relationships, and performance optimization.
Open MongoDB Shell using mongosh and
create a database named practiceDB.
mongosh
use practiceDB
Create a collection named students:
db.createCollection("students")
Insert the following sample records:
db.students.insertMany([
{
student_id: 101,
name: "Rahul",
age: 20,
course: "Java",
city: "Patna",
marks: 85
},
{
student_id: 102,
name: "Priya",
age: 21,
course: "Python",
city: "Gaya",
marks: 92
},
{
student_id: 103,
name: "Amit",
age: 19,
course: "Java",
city: "Patna",
marks: 76
},
{
student_id: 104,
name: "Neha",
age: 22,
course: "PHP",
city: "Delhi",
marks: 88
},
{
student_id: 105,
name: "Ravi",
age: 20,
course: "Python",
city: "Gaya",
marks: 67
}
])
Run the sample insertion only once. If you repeat it,
MongoDB may store duplicate student records because
student_id is not yet unique.
Assignment tasks:
Complete the following tasks:
schoolDB.teachers.courses.Useful commands:
use schoolDB
db.createCollection("teachers")
db.createCollection("courses")
show collections
db.teachers.find()
Remember that switching to a database with
use does not necessarily create it
immediately. MongoDB creates it when data is stored
or a collection is created.
insertOne().insertMany().email to one student.$unset to remove a field from a test document.Example syntax:
db.students.updateOne(
{ student_id: 101 },
{
$set: {
city: "Gaya"
}
}
)
Use $set to modify selected fields
without replacing the entire document.
deleteOne().deleteMany() and drop().Example syntax:
db.students.deleteOne({
student_id: 105
})
Be careful with delete operations. Always check the filter and use a practice collection when testing commands that delete many documents.
Write queries to solve these problems:
$in.$nin.Example:
db.students.find({
marks: { $gte: 80 }
})
$and to combine three conditions.$or to combine three conditions.Example:
db.students.find({
$and: [
{ city: "Patna" },
{ marks: { $gt: 75 } }
]
})
_id from the projected results.Example:
db.students.find(
{},
{
_id: 0,
name: 1,
marks: 1
}
).sort({
marks: -1
}).limit(3)
Create a collection named studentDetails:
db.studentDetails.insertMany([
{
student_id: 201,
name: "Karan",
skills: ["HTML", "CSS", "JavaScript"],
address: {
city: "Patna",
state: "Bihar"
}
},
{
student_id: 202,
name: "Simran",
skills: ["Python", "MongoDB"],
address: {
city: "Gaya",
state: "Bihar"
}
}
])
Assignment tasks:
$push.$addToSet.$pull.Example:
db.studentDetails.find({
"address.city": "Patna"
})
name field.city field.student_id.course and marks.explain("executionStats") to inspect a query.Example:
db.students.createIndex({
student_id: 1
})
Before creating a unique index, check for duplicate values. If duplicates exist, resolve them first.
Example: count students in each course.
db.students.aggregate([
{
$group: {
_id: "$course",
totalStudents: {
$sum: 1
}
}
},
{
$sort: {
totalStudents: -1
}
}
])
Example: calculate average marks by course.
db.students.aggregate([
{
$group: {
_id: "$course",
averageMarks: {
$avg: "$marks"
}
}
},
{
$sort: {
averageMarks: -1
}
}
])
Create a courses collection:
db.courses.insertMany([
{
course_id: 1,
course_name: "Java",
fee: 8000
},
{
course_id: 2,
course_name: "Python",
fee: 9000
},
{
course_id: 3,
course_name: "PHP",
fee: 7000
}
])
Assignment tasks:
$lookup to combine students with course details.
For a successful $lookup, both collections
must contain matching join fields with compatible
values and types.
$match in an aggregation pipeline.Example performance investigation:
db.students.find({
course: "Java"
}).explain("executionStats")
Build a student management database with these requirements:
studentDB.explain().Complete the project using MongoDB Shell or MongoDB Compass. Later, you can connect the database to an application using a MongoDB driver.
1. Which method inserts multiple documents?
2. Which aggregation stage groups documents?
3. Which option helps MongoDB find matching documents efficiently?