Within a MongoDB Collection I have a Date object that records both the date and time of an event. I am trying to query based on a specific date and to ignore the time i.e. show me all events for today.
Model.js
var EventSchema = new Schema({
eventName : String,
eventDate : Date,
eventLocation : String,
eventLocationGeo : String,
eventCreator : ObjectId
});
My main app.js is looking for the date to come across in Unix Timestamp format.
//Get Events based on Unix Time Stamp Date
app.get('/events/date/:date', passportConf.isAuthenticated, function(req, res){
var user = req.user._id;
var date = req.params.date;
console.log('date - ' + date);
eventController.getEventsByDate(user, date, req, res);
});
Typical REST call
http://localhost/events/date/1410764400
Most of the reference articles reference using the $gte and $lt but no where does it mention how to strip off the time so my queries end up pulling 24 hours based on the full Date AND time.
I would recommend using moment.unix(req.params.date).startOf('day').toDate() and moment.unix(req.params.date).endOf('day').toDate() from the moment.js library, which is phenomenally useful.
With just ECMAScript Date, you can do:
var start = new Date(req.params.date);
start.setHours(0);
start.setMinutes(0);
start.setSeconds(0);
start.setMilliseconds(0);