Bird
Raised Fist0
MongoDBquery~20 mins

sort method ascending and descending in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
MongoDB Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Sort documents by age ascending
Given a collection users with documents containing name and age, what is the output of this query?

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

Assume the collection has:
{name: 'Alice', age: 30}
{name: 'Bob', age: 25}
{name: 'Carol', age: 35}
MongoDB
db.users.find().sort({age: 1})
A[{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}]
B[{name: 'Carol', age: 35}, {name: 'Alice', age: 30}, {name: 'Bob', age: 25}]
C[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Carol', age: 35}]
D[{name: 'Bob', age: 25}, {name: 'Carol', age: 35}, {name: 'Alice', age: 30}]
Attempts:
2 left
💡 Hint
Sorting by age with 1 means ascending order.
query_result
intermediate
2:00remaining
Sort documents by age descending
Using the same users collection, what is the output of this query?

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

With documents:
{name: 'Alice', age: 30}
{name: 'Bob', age: 25}
{name: 'Carol', age: 35}
MongoDB
db.users.find().sort({age: -1})
A[{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}]
B[{name: 'Alice', age: 30}, {name: 'Carol', age: 35}, {name: 'Bob', age: 25}]
C[{name: 'Carol', age: 35}, {name: 'Bob', age: 25}, {name: 'Alice', age: 30}]
D[{name: 'Carol', age: 35}, {name: 'Alice', age: 30}, {name: 'Bob', age: 25}]
Attempts:
2 left
💡 Hint
Sorting by age with -1 means descending order.
🧠 Conceptual
advanced
1:30remaining
Understanding sort order values
In MongoDB, what do the values 1 and -1 mean when used in the sort() method?
A1 means ascending order, -1 means descending order
B1 means descending order, -1 means ascending order
C1 means sort by string length, -1 means sort by numeric value
D1 means no sorting, -1 means reverse sorting
Attempts:
2 left
💡 Hint
Think about smallest to largest and largest to smallest.
📝 Syntax
advanced
1:30remaining
Identify the invalid sort syntax
Which of the following MongoDB sort method calls will cause a syntax error?
Adb.collection.find().sort({age: 1})
Bdb.collection.find().sort({age: -1})
Cdb.collection.find().sort({age: 'asc'})
Ddb.collection.find().sort({age: 0})
Attempts:
2 left
💡 Hint
Sort values must be numeric 1 or -1.
optimization
expert
2:30remaining
Optimizing sort with index
You have a large MongoDB collection with an index on age. Which query will use the index efficiently for sorting by age descending?
Adb.collection.find().sort({age: 0})
Bdb.collection.find().sort({age: -1})
Cdb.collection.find().sort({name: 1})
Ddb.collection.find().sort({age: 1})
Attempts:
2 left
💡 Hint
Index on age supports sorting by age ascending or descending.

Practice

(1/5)
1. What does the sort({ age: 1 }) method do in MongoDB?
easy
A. Deletes documents with age less than 1
B. Sorts documents by age in descending order (largest to smallest)
C. Filters documents where age equals 1
D. Sorts documents by age in ascending order (smallest to largest)

Solution

  1. Step 1: Understand the sort method parameter

    The number 1 in sort({ age: 1 }) means ascending order.
  2. Step 2: Interpret the sorting effect

    Documents will be arranged from smallest age to largest age.
  3. Final Answer:

    Sorts documents by age in ascending order (smallest to largest) -> Option D
  4. Quick Check:

    sort({ field: 1 }) = ascending order [OK]
Hint: 1 means ascending order, -1 means descending order [OK]
Common Mistakes:
  • Confusing 1 with descending order
  • Thinking sort filters data
  • Assuming sort deletes documents
2. Which of the following is the correct syntax to sort documents by score in descending order in MongoDB?
easy
A. db.collection.find().sort({ score: -1 })
B. db.collection.find().sort({ score: 1 })
C. db.collection.find().sort(score: -1)
D. db.collection.find().sort({ -1: score })

Solution

  1. Step 1: Check the correct object syntax for sort

    The sort method requires an object with field name as key and 1 or -1 as value, inside curly braces.
  2. Step 2: Identify descending order syntax

    Descending order is indicated by -1, so { score: -1 } is correct.
  3. Final Answer:

    db.collection.find().sort({ score: -1 }) -> Option A
  4. Quick Check:

    sort({ field: -1 }) = descending order [OK]
Hint: Use curly braces and colon: sort({ field: -1 }) [OK]
Common Mistakes:
  • Missing curly braces around sort argument
  • Using parentheses instead of braces
  • Putting -1 as key instead of value
3. Given the documents: [{"name": "Anna", "score": 85}, {"name": "Ben", "score": 92}, {"name": "Cara", "score": 78}], what will be the order of names after running db.students.find().sort({ score: 1 })?
medium
A. ["Ben", "Anna", "Cara"]
B. ["Cara", "Ben", "Anna"]
C. ["Cara", "Anna", "Ben"]
D. ["Anna", "Cara", "Ben"]

Solution

  1. Step 1: Understand ascending sort by score

    Sorting by score ascending means from smallest to largest score: 78, 85, 92.
  2. Step 2: Map scores to names in order

    78 = Cara, 85 = Anna, 92 = Ben, so order is ["Cara", "Anna", "Ben"].
  3. Final Answer:

    ["Cara", "Anna", "Ben"] -> Option C
  4. Quick Check:

    sort({ score: 1 }) = ascending order [OK]
Hint: Ascending sort orders from smallest to largest [OK]
Common Mistakes:
  • Mixing ascending with descending order
  • Sorting by name instead of score
  • Confusing array order in output
4. What is wrong with this MongoDB query?
db.products.find().sort({ price: 2 })
medium
A. The find() method cannot be chained with sort()
B. The sort value must be 1 or -1, not 2
C. The field name 'price' is invalid
D. Missing parentheses after sort

Solution

  1. Step 1: Check valid sort values

    MongoDB sort only accepts 1 for ascending or -1 for descending, not 2.
  2. Step 2: Identify the error cause

    Using 2 will cause a syntax or runtime error because it's invalid.
  3. Final Answer:

    The sort value must be 1 or -1, not 2 -> Option B
  4. Quick Check:

    sort values = 1 or -1 only [OK]
Hint: Sort values must be exactly 1 or -1 [OK]
Common Mistakes:
  • Using numbers other than 1 or -1
  • Assuming any positive number works
  • Thinking sort can't chain after find()
5. You have a collection with documents containing category and price. How do you sort first by category ascending, then by price descending?
hard
A. db.collection.find().sort({ category: 1, price: -1 })
B. db.collection.find().sort({ price: -1, category: 1 })
C. db.collection.find().sort({ category: -1, price: 1 })
D. db.collection.find().sort({ category: 1 }).sort({ price: -1 })

Solution

  1. Step 1: Understand multi-field sort syntax

    MongoDB sorts by fields in the order they appear in the object passed to sort().
  2. Step 2: Apply ascending to category and descending to price

    Use { category: 1, price: -1 } to sort category ascending, then price descending within each category.
  3. Final Answer:

    db.collection.find().sort({ category: 1, price: -1 }) -> Option A
  4. Quick Check:

    Multi-field sort = object with fields in order [OK]
Hint: List fields in sort object in desired priority order [OK]
Common Mistakes:
  • Reversing field order changes sort priority
  • Trying to chain multiple sort() calls
  • Using wrong sort values for fields