MongoDB Sorting Practice Questions with Solutions

Introduction

Sorting in MongoDB is used to arrange query results in a specific order. You can sort documents by numbers, text, dates, or multiple fields. MongoDB uses the sort() method, where 1 means ascending order and -1 means descending order. In this chapter, you will practice sorting students, products, employees, and other documents using single and multiple fields. MongoDB Sorting practice questions with solutions to help you understand the concepts.

Q1. Sort Students by Age in Ascending Order

Problem Statement:
Find all students and arrange them from the youngest to the oldest.

MongoDB Query:

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

Expected Output:

{ name: "Aman", age: 14 }
{ name: "Rahul", age: 16 }
{ name: "Priya", age: 17 }
{ name: "Neha", age: 18 }

Explanation:
1 means ascending order, so smaller age values appear first.


Q2. Sort Students by Age in Descending Order

Problem Statement:
Find all students and arrange them from the oldest to the youngest.

MongoDB Query:

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

Expected Output:

{ name: "Neha", age: 18 }
{ name: "Priya", age: 17 }
{ name: "Rahul", age: 16 }
{ name: "Aman", age: 14 }

Explanation:
-1 means descending order, so larger age values appear first.


Q3. Sort Students Alphabetically by Name

Problem Statement:
Find all students and arrange their names in alphabetical order.

MongoDB Query:

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

Expected Output:

{ name: "Aman", age: 14 }
{ name: "Neha", age: 18 }
{ name: "Priya", age: 17 }
{ name: "Rahul", age: 16 }

Explanation:
MongoDB can sort string fields as well. Using name: 1 sorts names in ascending order.


Q4. Sort Students by Name in Reverse Alphabetical Order

Problem Statement:
Find all students and arrange their names from Z to A.

MongoDB Query:

db.students.find().sort({
  name: -1
})

Expected Output:

{ name: "Rahul", age: 16 }
{ name: "Priya", age: 17 }
{ name: "Neha", age: 18 }
{ name: "Aman", age: 14 }

Explanation:
name: -1 sorts the names in descending alphabetical order.


Q5. Sort Products by Price from Low to High

Problem Statement:
Suppose the products collection contains product prices. Find all products and display the cheapest products first.

MongoDB Query:

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

Expected Output:

{ name: "Mouse", price: 500 }
{ name: "Keyboard", price: 1200 }
{ name: "Monitor", price: 8000 }
{ name: "Laptop", price: 55000 }

Explanation:
price: 1 sorts products from the lowest price to the highest price.


Q6. Sort Products by Price from High to Low

Problem Statement:
Find all products and display the most expensive products first.

MongoDB Query:

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

Expected Output:

{ name: "Laptop", price: 55000 }
{ name: "Monitor", price: 8000 }
{ name: "Keyboard", price: 1200 }
{ name: "Mouse", price: 500 }

Explanation:
price: -1 sorts the products in descending price order.


Q7. Sort Students by Course and Age

Problem Statement:
Sort students first by course alphabetically and then by age from youngest to oldest within each course.

MongoDB Query:

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

Expected Output:

{ name: "Priya", course: "Java", age: 17 }
{ name: "Aman", course: "Python", age: 14 }
{ name: "Rahul", course: "Python", age: 16 }

Explanation:
MongoDB can sort using more than one field.

course: 1

is the primary sorting field.

age: 1

is used when documents have the same course.


Q8. Sort Students by Course Ascending and Age Descending

Problem Statement:
Sort students alphabetically by course. If multiple students have the same course, show the older student first.

MongoDB Query:

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

Expected Output:

{ name: "Priya", course: "Java", age: 17 }
{ name: "Rahul", course: "Python", age: 16 }
{ name: "Aman", course: "Python", age: 14 }

Explanation:
Different sort directions can be used for different fields.

  • course: 1 → ascending
  • age: -1 → descending

Q9. Find Python Students and Sort by Age

Problem Statement:
Find students enrolled in Python and arrange them from youngest to oldest.

MongoDB Query:

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

Expected Output:

{ name: "Aman", course: "Python", age: 14 }
{ name: "Rahul", course: "Python", age: 16 }

Explanation:
The query first filters the documents:

{ course: "Python" }

Then it sorts the matching documents:

.sort({ age: 1 })

Q10. Sort Students by Marks and Show the Top 3

Problem Statement:
Find the three students with the highest marks.

Suppose the documents contain a marks field.

MongoDB Query:

db.students.find().sort({
  marks: -1
}).limit(3)

Expected Output:

{ name: "Neha", marks: 95 }
{ name: "Rahul", marks: 91 }
{ name: "Priya", marks: 87 }

Explanation:
The query performs two operations:

  1. sort({ marks: -1 }) → highest marks first.
  2. limit(3) → return only the first three documents.

This is a very common pattern for finding top performers.

Key Takeaways

  • MongoDB uses the sort() method to arrange query results.
  • 1 means ascending order.
  • -1 means descending order.
  • Numbers can be sorted from low to high or high to low.
  • Strings can be sorted alphabetically.
  • Multiple fields can be used in a single sort() operation.
  • Different fields can have different sorting directions.
  • sort() can be combined with find() filters.
  • sort() can be combined with limit() to find top or bottom records.
  • Sorting changes the order of returned results, not the stored documents.

FAQs

1. How do you sort documents in MongoDB?

Use the sort() method with find():

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

2. What does 1 mean in MongoDB sorting?

1 means ascending order.

For example:

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

This displays lower ages before higher ages.

3. What does -1 mean in MongoDB sorting?

-1 means descending order.

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

This displays higher ages before lower ages.

4. Can MongoDB sort documents by multiple fields?

Yes. You can provide multiple fields:

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

MongoDB sorts by course first and uses age when needed.

5. Can sorting be combined with filtering?

Yes. For example:

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

MongoDB first finds Python students and then sorts those results by age.

6. Can sort() and limit() be used together?

Yes. This is useful for finding top or bottom records.

db.students.find().sort({
  marks: -1
}).limit(3)

This returns the three students with the highest marks.

7. Does MongoDB sorting change the original documents?

No. sort() only changes the order of the query results. It does not modify or rearrange the documents stored in the collection.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top