๐งช MongoDB Practice Questions with Solutions (Beginner to Advanced)
Learning MongoDB is not just about reading concepts—it’s about practicing real problems.
Here, you’ll solve hands-on MongoDB exercises step by step. Each question includes:
- ✅ Problem
- ✅ Solution
- ✅ Explanation
By the end, you’ll feel confident working with real-world MongoDB scenarios.
๐ข Level 1: Beginner Exercises
✅ Q1: Create a Database and Collection
Problem:
Create a database called school and a collection called students.
Solution:
use school
db.createCollection("students")
Explanation:
use school creates (or switches to) the database and createCollection() creates a new collection.
✅ Q2: Insert Documents
Problem:
Insert 3 student records with fields: name, age, course.
Solution:
db.students.insertMany([
{ name: "Amit", age: 20, course: "BCA" },
{ name: "Sara", age: 22, course: "MCA" },
{ name: "John", age: 19, course: "BSc" }
])
Explanation:
insertMany() allows inserting multiple documents at once.
✅ Q3: Find All Documents
Problem:
Retrieve all students from the collection.
Solution:
db.students.find()
Explanation:
find() returns all documents in the collection.
✅ Q4: Filter Data
Problem:
Find students with age greater than 20.
Solution:
db.students.find({ age: { $gt: 20 } })
Explanation:
$gt means "greater than".
๐ก Level 2: Intermediate Exercises
✅ Q5: Update a Document
Problem:
Update "Amit"’s course to "B.Tech".
Solution:
db.students.updateOne(
{ name: "Amit" },
{ $set: { course: "B.Tech" } }
)
Explanation:
$set updates specific fields.
✅ Q6: Delete a Document
Problem:
Delete the student named "John".
Solution:
db.students.deleteOne({ name: "John" })
Explanation:
deleteOne() removes one matching document.
✅ Q7: Sort Data
Problem:
Sort students by age in descending order.
Solution:
db.students.find().sort({ age: -1 })
Explanation:
-1 = descending, 1 = ascending.
✅ Q8: Limit Results
Problem:
Show only 2 students.
Solution:
db.students.find().limit(2)
Explanation:
limit() restricts number of results.
๐ต Level 3: Advanced Exercises
✅ Q9: Design a Schema (Embedding vs Referencing)
Problem:
Design a schema for Users and Orders where each user can have many orders.
Solution:
// Users Collection
{
_id: 1,
name: "Amit"
}
// Orders Collection
{
_id: 101,
user_id: 1,
product: "Laptop",
price: 50000
}
Explanation:
Use referencing when data grows large. Avoid embedding too many orders inside a single document.
✅ Q10: Aggregation Query
Problem:
Find the average age of students.
Solution:
db.students.aggregate([
{
$group: {
_id: null,
averageAge: { $avg: "$age" }
}
}
])
Explanation:
$group is used for aggregation and $avg calculates average.
✅ Q11: Find Duplicate Data
Problem:
Find duplicate student names.
Solution:
db.students.aggregate([
{
$group: {
_id: "$name",
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
}
])
Explanation:
Groups data by name and filters duplicates using $match.
๐ Bonus Challenge
Challenge:
Design a database for a blog system with Users, Posts, and Comments.
- Which data will you embed?
- Which data will you reference?
- Why?
๐ฏ Final Thoughts
If you completed these exercises, you now understand:
- CRUD operations (Create, Read, Update & Delete)
- Filtering & sorting
- Aggregation
- Schema design basics
The best way to learn MongoDB is:
Practice → Build → Break → Fix → Repeat
๐ What to Learn Next
- MongoDB with Python (PyMongo)
- Indexing for performance
- Real-world projects
๐ฌ Your Turn:
Which question did you find most challenging? Let us know in the comments!
No comments:
Post a Comment