How can I sort a minimongo cursor by date?

110 Views Asked by At

I would like to sort a minimongo cursor using a date field. It appears that $orderBy will be deprecated according to the mongoDB documentation (not that I know if it works with minimongo) and sort() does not seem to work.

Is there a way to sort a minimongo cursor using a date field or should I rely on plain Javascript/underscore (Sorting by date with underscore.js or just plain JS)?

1

There are 1 best solutions below

1
On

It turns out that sort is supported, I just didn't get the syntax right. It would look like this:

myCollection.find({},{sort:{date:1}); // for older to newer items

Edit: more generally, 1 is for ascending and -1 is for descending. (for future readers, as noted by @BlazeSahlzen in comments)