MongoDB Sorting, Limiting & Pagination Practice Exercises (Part-4)

🧩 MongoDB Practice Series – Sorting, Limiting & Pagination Exercises (Part 4)


Welcome to Part 4 of the MongoDB Practice Series.

In this lesson, you will practice:

  • Sorting data
  • Limiting results
  • Using skip for pagination
  • Combining filters with sorting

These skills are very important in real-world applications like websites and dashboards.


🟢 Exercise 1: Sort in Ascending Order

Problem:
Retrieve all students and sort them by age in ascending order.

Solution:


db.students.find().sort({ age: 1 })

Learning Goal: Use 1 for ascending order.


🟢 Exercise 2: Sort in Descending Order

Problem:
Retrieve all products and sort them by price from highest to lowest.

Solution:


db.products.find().sort({ price: -1 })

Learning Goal: Use -1 for descending order.


🟡 Exercise 3: Limit Results

Problem:
Show only the top 3 highest-priced products.

Solution:


db.products.find().sort({ price: -1 }).limit(3)

Learning Goal: Combine sorting with limiting.


🟡 Exercise 4: Pagination Using Skip

Problem:
Display the second page of results where each page shows 5 records.

Solution:


db.students.find()
  .skip(5)
  .limit(5)

Learning Goal: Understand basic pagination.

Explanation:
- skip(5) skips the first 5 records.
- limit(5) shows the next 5 records.


🔵 Exercise 5: Filter + Sort

Problem:
Find students with age greater than 18 and sort them by name in alphabetical order.

Solution:


db.students.find({
  age: { $gt: 18 }
}).sort({ name: 1 })

Learning Goal: Combine filtering with sorting.


🔵 Exercise 6: Get Latest Records

Problem:
Assume each document has a field called createdAt. Retrieve the 5 most recently added documents.

Solution:


db.posts.find()
  .sort({ createdAt: -1 })
  .limit(5)

Learning Goal: This pattern is commonly used in blogs and news websites.


🔴 Exercise 7: Complex Real-World Scenario

Problem:
From the products collection:

  • Find products where price is greater than 1000
  • Sort them by price (highest first)
  • Show only top 2 results

Solution:


db.products.find({
  price: { $gt: 1000 }
})
.sort({ price: -1 })
.limit(2)

Learning Goal: Practice combining multiple concepts in one query.


🚀 Mini Challenge

Create a collection called orders with fields:

  • orderId
  • customerName
  • totalAmount
  • orderDate

Then write queries to:

  • Show the latest 5 orders
  • Sort orders by totalAmount (highest first)
  • Display page 2 if each page contains 3 orders

Try solving without looking back at the examples.


🎯 What’s Next?

In Part 5 of the MongoDB Practice Series, we will practice:

  • Aggregation challenges
  • $group operations
  • Calculating totals and averages
  • Real-world reporting queries

Stay tuned for the next level!


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!


Featured Post

MongoDB Aggregation Practice Challenges with Solutions (Part - 5)

🧩 MongoDB Practice Series – Aggregation Challenges with Solutions (Part 5) ← Part 4 MongoDB Tutorial Next → Welcome to Part 5 ...

Popular Posts