How can I sort a Meteor collection by time of insertion ?

How can I sort a Meteor collection by time of insertion ?

Asked on November 15, 2018 in Meteor.
Add Comment


  • 2 Answer(s)

    Here is the solution is:

    •  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.

    Answered on November 15, 2018.
    Add Comment

    By using alternate method,

    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.

    Here is an Code:

    Template.list.aphorisms = function () {
        return Aphorisms.find().fetch().reverse();
    };
    
    Answered on November 15, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.