Sorting Flashcards
(5 cards)
1
Q
How do you use a URL to send a sorting query?
A
URL?sort=attr,atrr,atrr
2
Q
What do the attributes after the first attribute mean in a sort request?
A
They are the attributes used to sort in case of a tie breaker from sorting using the previous attributes
3
Q
In what order is sorting done by default?
A
Ascending order
4
Q
How would you send a sorting request in descending order to MongoDB?
A
by adding a negative sign before the attribute you wish to sort with e.g. URL?sort=-attr
5
Q
Write the code for handling sorting in Mongoose.
A
if(req.query.sort){const x = req.query.sort.split(‘,’).join(‘ ‘);query = query.sort(x)}else{query = query.sort(‘default attr’);}