Reputation: 365
I am trying to get a count on posted item from meteor-mongo
matching date periods.
I inserted my posts dates as such.
posts
submitted: new Date()
the dates in the database have the following format.
yyyy-mm-dd 16:16:34.317Z // I do not understand the last part (what format it is)
I have tried this to get match the date of today
from the submitted
field
var currentDate = new Date();
var dd = currentDate.getDate();
var mm = currentDate.getMonth()+1;
var yyyy = currentDate.getFullYear();
var today = yyyy+'-'+mm+'-'+dd;
Posts.find({submitted: today}).count()
However, the last part is returning 0
.
Is it because the last hh,mm,ss part of today is missing? If so, how can I tell meteor-mongo
to ignore the time part of date so that I can return that count?
Upvotes: 1
Views: 387
Reputation: 11376
I don't like to deal with JS date objects formats, and i guess you either (I do not understand the last part (what format it is))
Give a try to momentjs package, the documentation its pretty clear.
So on the insert you can have something like.
var today = moment().format(''MMMM Do YYYY, h:mm:ss a'); // April 3rd 2015, 12:17:06 pm
Posts.insert({subbmited:today})
and do a simple find like this.
var endQuery = today..add('days', 3).calendar(); // just an example.
Posts.find({submitted: {$gt: today,$lt:endQuery}})
Here thanks to @Larry Maccherone point we are using $gte (grater than) and $lt (less than), so this works like find me post between the post submitted day and the post submitted dat + 3 days (like you ask managing ranges)
Upvotes: 1
Reputation: 681
You are storing your submitted date in MongoDB with both time and timezone information. Performing a direct comparison of your currentDate
with the submitted
date in Mongo will never be true, since your currentDate
does not contain time information.
Also you are using a String
data type to query the date, which also will not work since you need to use a Date
data type. Something like this will work for you:
var today = new Date();
today.setHours(0, 0, 0, 0);
Posts.find({submitted: {$gt: today}})
Which will return all the posts with a date greater than midnight of today's date.
Upvotes: 1