62

I am working on my first project using Meteor, and am having some difficulty with sorting.

I have a form where users enter aphorisms that are then displayed in a list. Currently the most recent aphorisms automatically display at the bottom of the list. Is there an easy way to have the most recent appear at the top of the list instead?

I tried:

   Template.list.aphorisms = function () {
    return Aphorisms.find({}, {sort: {$natural:1}});
};

And am stumped because the Meteor docs don't have many examples.

Coleman
  • 565
  • 7
  • 15
squeezemylime
  • 2,102
  • 3
  • 18
  • 26

2 Answers2

110

Assuming that the date_created is in a valid date format along with the timestamp, You should insert the parsed value of date_created using Date.parse() javascript function, which gives the number of milliseconds between January 1, 1970 and date value contained in date_created.

As a result of that, the most recently added record will contain greater value of date_created than the record inserted before it.

Now when fetching the records, sort the cursor in descending order of the date_created parameter as:

 Aphorisms.find({}, {sort: {date_created: -1}});

This will sort records from newer to older.

starball
  • 20,030
  • 7
  • 43
  • 238
sohel khalifa
  • 5,602
  • 3
  • 34
  • 46
  • This works very well..the only way I am currently only able to store the date in a way that works is: var combinedTime = year + "" + day + "" + month + "" + hour + "" + minute + "" + second; – squeezemylime Dec 20 '12 at 14:38
  • Yeah, make sure `date_created` attribute contains a valid timestamp too. Since this will not work with only `date+month+year` . – sohel khalifa Dec 21 '12 at 04:21
  • 1
    In order to sort on dates client-side in minimongo, the date must be stored as a string, such as ISO8601 format. Minimongo does not know how to sort on Date types. See the text in red under http://docs.meteor.com/#collections – David Wihl Dec 21 '12 at 09:09
  • It changed a bit since '12: `date_created` is now `createdAt` – Erdal G. Nov 12 '15 at 11:34
8

I've found the following to be a cleaner solution:

   Template.list.aphorisms = function () {
      return Aphorisms.find().fetch().reverse();
   };

Given that entire collection already exists in the reverse order that you would like, you can simply create an array of all the objects and reverse the order.

Samuel Ellis
  • 157
  • 2
  • 5
  • 3
    I'm not sure you can actually rely on Mongo keeping your items in order. Theoretically, yes, this works — but to the best of my knowledge, this is not always a safe assumption. – j6m8 Jul 01 '15 at 23:11
  • 2
    This is the safest solution. MongoDB automatically creates a createdAt field by default in Meteor. So, when making large queries this is actually the safest and cleanest solution. – TheBetterJORT Aug 19 '15 at 18:04
  • 2
    Hi, `createdAt` field is not created automatically (maybe it was at the time of your comment though, I use 1.4.2). You have to handle it by yourself, usually thanks to `aldeed:collection2` package. I am not sure why this is not the default behaviour, as with mongoose or other languages. – Eric Burel Dec 02 '16 at 14:59
  • You can not rely on the fact that the find will get the documents in creation order – MickaelFM Sep 20 '19 at 15:06