on calling db.distinct() method always return the data in ascending order sorted. Is their a way I can get the data sorted in descending order?
Asked
Active
Viewed 566 times
0
-
1If you want to sort it ou need to use $group in the aggregation framework instead – Sammaye Aug 26 '13 at 11:10
1 Answers
1
The distinct command returns a javascript array, so you can always sort it and reverse it:
var array = db.coll.distinct("{field_name}")
array.sort()
array.reverse()
If using java driver, you can do something such as:
List list = coll.distinct("{field_name}");
Collections.sort(list);
Collections.reverse(list);

Ori Dar
- 18,687
- 5
- 58
- 72
-
Note that JS sorting is not always the fastest thing, plus no possibility ever of uisng indexes – Sammaye Aug 26 '13 at 15:33