Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does sorting by multiple fields mean in MongoDB?
It means arranging documents in order based on more than one field, for example, first by age, then by name.
Click to reveal answer
beginner
How do you specify sorting order for multiple fields in MongoDB?
You provide an object to sort() with field names as keys and 1 for ascending or -1 for descending as values, like { age: 1, name: -1 }.
Click to reveal answer
beginner
What happens if two documents have the same value in the first sorted field?
MongoDB uses the next field in the sort order to decide which document comes first.
Click to reveal answer
intermediate
Write a MongoDB query to sort users first by score descending, then by username ascending.
db.users.find().sort({ score: -1, username: 1 })
Click to reveal answer
beginner
Why is sorting by multiple fields useful in real life?
It helps organize data clearly, like sorting a contact list by last name and then first name, so people with the same last name are grouped and ordered by first name.
Click to reveal answer
In MongoDB, how do you sort documents by two fields, age ascending and name descending?
Asort({ age: 'asc', name: 'desc' })
Bsort({ age: -1, name: 1 })
Csort({ age: 1, name: -1 })
Dsort(['age', 'name'])
✗ Incorrect
Use 1 for ascending and -1 for descending in the sort object.
What does the number -1 mean in a MongoDB sort object?
ASort descending
BSort ascending
CIgnore the field
DSort randomly
✗ Incorrect
In MongoDB, -1 means descending order.
If two documents have the same value in the first sorted field, what does MongoDB do?
ADeletes one document
BReturns an error
CLeaves them unordered
DSorts by the next field in the sort object
✗ Incorrect
MongoDB uses the next field to break ties.
Which of these is a valid MongoDB sort command to sort by price ascending and rating descending?
Asort({ price: 1, rating: -1 })
Bsort({ price: -1, rating: 1 })
Csort({ price: 'asc', rating: 'desc' })
Dsort(price, rating)
✗ Incorrect
Use 1 for ascending and -1 for descending in the sort object.
Why might you want to sort by multiple fields in a database?
ATo make data look random
BTo organize data clearly when one field alone is not enough
CTo delete duplicate data
DTo speed up queries
✗ Incorrect
Sorting by multiple fields helps organize data better when one field is not enough.
Explain how to sort documents by multiple fields in MongoDB and why it is useful.
Think about sorting by age then name.
You got /4 concepts.
Write a MongoDB query to sort a collection by two fields: first descending by date, then ascending by title.
Use 1 for ascending and -1 for descending.
You got /2 concepts.
Practice
(1/5)
1. What does sorting by multiple fields in MongoDB allow you to do?
easy
A. Organize data by more than one field in a specific order
B. Delete multiple fields from documents
C. Create new fields based on existing ones
D. Filter documents by multiple conditions
Solution
Step 1: Understand sorting purpose
Sorting arranges documents in order based on field values.
Step 2: Recognize multiple fields effect
Sorting by multiple fields means ordering by the first field, then by the second if the first is equal, and so on.
Final Answer:
Organize data by more than one field in a specific order -> Option A
Quick Check:
Sorting by multiple fields = Organize data by multiple fields [OK]
Hint: Sorting multiple fields orders by priority fields [OK]
Common Mistakes:
Confusing sorting with filtering
Thinking sorting creates or deletes fields
Assuming sorting only works on one field
2. Which of the following is the correct syntax to sort documents by age ascending and name descending in MongoDB?
easy
A. db.collection.find().sort({name: 1, age: -1})
B. db.collection.find().sort({age: -1, name: 1})
C. db.collection.find().sort({age: 1, name: -1})
D. db.collection.find().sort({age: 'asc', name: 'desc'})
Solution
Step 1: Recall sort syntax
MongoDB uses 1 for ascending and -1 for descending order in sort objects.
Step 2: Match fields and order
Sorting by age ascending (1) and name descending (-1) matches db.collection.find().sort({age: 1, name: -1}).
Final Answer:
db.collection.find().sort({age: 1, name: -1}) -> Option C
Hint: Sort priority follows field order in sort object [OK]
Common Mistakes:
Ignoring order of fields in sort
Mixing ascending and descending incorrectly
Assuming score sorts descending by default
4. Identify the error in this MongoDB sort query:
db.collection.find().sort({age: 1, name: 2})
medium
A. Using 2 instead of -1 or 1 for sorting order
B. Missing parentheses after find()
C. Using curly braces instead of square brackets
D. Sorting fields must be strings, not numbers
Solution
Step 1: Check valid sort values
MongoDB accepts only 1 (ascending) or -1 (descending) as sort values.
Step 2: Identify invalid value
Value 2 is invalid and causes syntax error.
Final Answer:
Using 2 instead of -1 or 1 for sorting order -> Option A
Quick Check:
Sort values must be 1 or -1 [OK]
Hint: Sort values must be 1 or -1, never other numbers [OK]
Common Mistakes:
Using numbers other than 1 or -1
Confusing sort object syntax
Assuming 2 means descending
5. You want to sort a collection by department ascending, then by salary descending, but only for employees with the same department. Which MongoDB query correctly achieves this?
hard
A. db.employees.find().sort({salary: -1, department: 1})
B. db.employees.find().sort({department: 1, salary: -1})
C. db.employees.find().sort({department: -1, salary: 1})
D. db.employees.find().sort({salary: 1, department: -1})
Solution
Step 1: Understand sorting priority
Sorting by department ascending means all employees grouped by department alphabetically.
Step 2: Sort salary descending within each department
Within each department group, employees are ordered by salary from highest to lowest.
Step 3: Match correct sort order
db.employees.find().sort({department: 1, salary: -1}) matches department:1 (ascending) and salary:-1 (descending).
Final Answer:
db.employees.find().sort({department: 1, salary: -1}) -> Option B
Quick Check:
Sort by department↑ then salary↓ = db.employees.find().sort({department: 1, salary: -1}) [OK]
Hint: Order fields in sort by priority, use 1 or -1 for direction [OK]
Common Mistakes:
Reversing field order in sort object
Using wrong sort directions
Assuming sorting salary first groups by salary only