MongoDB Filtering and Query Operators Practice with Solutions

🧩 MongoDB Practice Series – Filtering & Query Logic Exercises (Part 3)


Welcome to Part 3 of the MongoDB Practice Series.

In this lesson, you will practice advanced filtering techniques using MongoDB query operators.

These exercises will help you write more powerful and flexible queries.


🟢 Exercise 1: Use Comparison Operators

Problem:
Find all products where price is greater than 500.

Solution:


db.products.find({
  price: { $gt: 500 }
})

Learning Goal: Practice using $gt (greater than).


🟡 Exercise 2: Range Query

Problem:
Find students whose age is between 18 and 25 (inclusive).

Solution:


db.students.find({
  age: { $gte: 18, $lte: 25 }
})

Learning Goal: Combine $gte and $lte for range filtering.


🟡 Exercise 3: Use $in Operator

Problem:
Find courses where level is either "Beginner" or "Intermediate".

Solution:


db.courses.find({
  level: { $in: ["Beginner", "Intermediate"] }
})

Learning Goal: Use $in for matching multiple values.


🔵 Exercise 4: Use Logical AND

Problem:
Find students who are older than 20 AND enrolled in "MCA".

Solution:


db.students.find({
  age: { $gt: 20 },
  course: "MCA"
})

Learning Goal: Understand that multiple conditions act as AND in MongoDB.


🔵 Exercise 5: Use $or Operator

Problem:
Find students who are either younger than 18 OR older than 25.

Solution:


db.students.find({
  $or: [
    { age: { $lt: 18 } },
    { age: { $gt: 25 } }
  ]
})

Learning Goal: Use $or for alternative conditions.


🔴 Exercise 6: Exclude Specific Fields

Problem:
Retrieve all students but exclude the _id field.

Solution:


db.students.find(
  {},
  { _id: 0 }
)

Learning Goal: Practice projection in queries.


🔴 Exercise 7: Search by Partial Match

Problem:
Find products where the name contains the word "Pro".

Solution:


db.products.find({
  name: { $regex: "Pro" }
})

Learning Goal: Learn basic pattern matching using $regex.


🚀 Mini Challenge

Create a collection called employees with fields:

  • name
  • salary
  • department
  • experience

Then write queries to:

  • Find employees with salary above 50,000
  • Find employees in either "IT" or "HR"
  • Find employees with experience between 2 and 5 years

Try solving without checking the examples above.


🎯 What’s Next?

In Part 4 of the MongoDB Practice Series, we will learn:

  • Sorting data
  • Limiting results
  • Advanced query combinations
  • Real-world filtering scenarios

Stay tuned for the next challenge!


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

Next: Part 3 of the MongoDB Practice Series.


Featured Post

MongoDB Filtering and Query Operators Practice with Solutions

🧩 MongoDB Practice Series – Filtering & Query Logic Exercises (Part 3) Welcome to Part 3 of the MongoDB Practice Series. In this ...

Popular Posts