Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding most recent entry in a meteor collection

Tags:

meteor

My task sounds simple, but I can't nail down how to write this... I simply want to find the most recent item inserted into a collection and display it on my meteor app.

I've been playing with Collection.find() with no real results. This is the last line I've tried: (keep in mind these names are placeholder. My collection is not called Collection in my code)

Template.tempname.tempitem = function () {
    return Collection.find({}, {sort: {DateTime: -1, limit: 1,}});
};

I have a feild in the collection that is the result of

Date.parse(Date())

which should allow me to sort by most recent. There is likely other ways to do this, but this was the first solution I came up with.

like image 213
ParadoxCTRL Avatar asked Jun 01 '14 14:06

ParadoxCTRL


1 Answers

As per the docs you can also do:

return Collection.findOne({}, {sort: {DateTime: -1, limit: 1}});

Provided that DateTime is this field that you do Date.parse(Date()). Also you don't have to store dates as unix timestamps you can just do new Date() and it would be stored as the Date object type.

like image 142
Tarang Avatar answered Sep 28 '22 07:09

Tarang