0

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?

Phalguni Mukherjee
  • 623
  • 3
  • 11
  • 29

1 Answers1

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