Reputation: 167
I have a Mongo Collection called Posts
which I'm using in my Meteor project in which each element has a field called date
which holds the date at which the entry was created.
I need a query which I can execute on my collection that will return only entries which have been created in the last 24 hours.
I have the following query:
Posts.find({date: {$gt: new Date("Mar 10, 2016")}});
Which works to return the posts that were created on March 10th, However, I think I need to replace Mar 10, 2016
with the date 24 hours ago in order to achieve what I want, but I don't know how to do that.
Upvotes: 1
Views: 175
Reputation: 64312
Without installing any packages:
var d = new Date();
d.setDate(d.getDate() - 1);
Posts.find({date: {$gt: d}});
With moment:
Posts.find({date: {$gt: moment().subtract(1, 'day').toDate()}});
Upvotes: 2
Reputation: 20227
You can use the momentjs package to easily do date math in js:
Posts.find({ date: { $gt: moment().subtract(24,'hours').toDate() }});
Upvotes: 0