MongoDB CRUD Operations Practice Exercises with Solutions

🧩 MongoDB Practice Series – CRUD Operations Exercises with Solutions


This is Part 2 of our MongoDB Practice Series.

In this lesson, you will practice real-world CRUD operations (Create, Read, Update, Delete).

These exercises will improve your confidence in handling MongoDB data.


🟢 Exercise 1: Insert Student Records

Problem:
Create a collection called courses and insert 3 course documents with fields: title, duration, and level.

Solution:


use practiceDB

db.createCollection("courses")

db.courses.insertMany([
  { title: "Python Basics", duration: "4 weeks", level: "Beginner" },
  { title: "MongoDB Fundamentals", duration: "3 weeks", level: "Beginner" },
  { title: "Advanced Database Design", duration: "6 weeks", level: "Advanced" }
])

Learning Goal: Practice creating collections and inserting multiple documents.


🟡 Exercise 2: Read with Multiple Conditions

Problem:
Find all courses where level is "Beginner" AND duration is "4 weeks".

Solution:


db.courses.find({
  level: "Beginner",
  duration: "4 weeks"
})

Learning Goal: Understand how MongoDB handles multiple conditions in queries.


🟡 Exercise 3: Update Multiple Documents

Problem:
Increase duration of all Beginner courses to "5 weeks".

Solution:


db.courses.updateMany(
  { level: "Beginner" },
  { $set: { duration: "5 weeks" } }
)

Learning Goal: Use updateMany() for bulk updates.


🔵 Exercise 4: Add New Field to Existing Documents

Problem:
Add a new field called price to all courses and set it to 1000.

Solution:


db.courses.updateMany(
  {},
  { $set: { price: 1000 } }
)

Learning Goal: Learn how to modify existing schema safely.


🔴 Exercise 5: Delete Based on Condition

Problem:
Delete all courses where level is "Advanced".

Solution:


db.courses.deleteMany({ level: "Advanced" })

Learning Goal: Understand controlled deletion using filters.


🚀 Mini Challenge

Create a collection called books with:

  • title
  • author
  • year
  • category

Then:

  • Insert 3 books
  • Update one book
  • Delete one book

Try solving it without looking at the previous examples.

Also, Have a look at MongoDB MCQs


🎯 Next in the Series

In the next part, we will practice:

  • Filtering with operators ($gt, $lt, $in)
  • Sorting and limiting
  • Real-world query scenarios

Stay tuned for Part 3 of the MongoDB Practice Series.


MongoDB MCQ Questions and Answers (Beginner to Advanced Quiz)

MongoDB MCQ Questions and Answers (Beginner to Advanced)


Test your MongoDB knowledge with these multiple choice questions (MCQs).

This quiz is designed to help you revise:

Each question includes the correct answer and explanation.


🟢 Beginner Level

Q1. Which command is used to create a database in MongoDB?

A) create db
B) use db
C) make database
D) new db

Answer: B) use db

Explanation: MongoDB creates a database when you use it and insert data.


Q2. Which method is used to insert multiple documents?

A) insertOne()
B) insertAll()
C) insertMany()
D) addMany()

Answer: C) insertMany()


Q3. Which operator is used for “greater than”?

A) $lt
B) $gt
C) $eq
D) $ne

Answer: B) $gt


🟡 Intermediate Level

Q4. What does $in operator do?

A) Matches exact value
B) Matches multiple values
C) Matches range
D) Matches null

Answer: B) Matches multiple values


Q5. Which method is used to update multiple documents?

A) updateOne()
B) updateAll()
C) updateMany()
D) modify()

Answer: C) updateMany()


Q6. What does limit() do?

A) Filters data
B) Sorts data
C) Limits number of documents
D) Groups data

Answer: C) Limits number of documents


🔵 Advanced Level

Q7. Which stage is used in aggregation to group data?

A) $match
B) $group
C) $sort
D) $limit

Answer: B) $group


Q8. What is the purpose of an index?

A) Store data
B) Improve performance
C) Delete data
D) Backup data

Answer: B) Improve performance


Q9. Which function shows query execution details?

A) analyze()
B) debug()
C) explain()
D) stats()

Answer: C) explain()


🔴 Expert Level

Q10. When should you use embedding?

A) When data grows large
B) When data is unrelated
C) When data is small and related
D) Always

Answer: C) When data is small and related


Q11. What happens if a document exceeds size limit?

A) MongoDB compresses it
B) MongoDB deletes it
C) It throws an error
D) It splits automatically

Answer: C) It throws an error


Q12. Which is better for large datasets?

A) Embedding
B) Referencing
C) Both same
D) None

Answer: B) Referencing


🏆 Final Score

Count your correct answers:

  • 10–12 → Excellent 🎯
  • 7–9 → Good 👍
  • 4–6 → Keep practicing 📘
  • 0–3 → Revise basics 🔁

🚀 What's Next?

Want more practice?

  • Try MongoDB Practice Series (Parts 1–8)
  • Build your own mini project
  • Practice real-world queries

💬 Comment your score below!


Featured Post

MongoDB CRUD Operations Practice Exercises with Solutions

🧩 MongoDB Practice Series – CRUD Operations Exercises with Solutions This is Part 2 of our MongoDB Practice Series. In this lesson, yo...

Popular Posts