1

My events colleciton stores the following documents:

{
  "_id": {
      "$oid": "537b2a232a47f21830ae5b7b"
  },
  "startTime": {
      "$date": "2014-05-18T21:00:00.000Z"
  },
  "endTime": {
      "$date": "2014-05-18T23:00:00.000Z"
  }
},
{
  "_id": {
    "$oid": "537af8136c4162d0379e2139"
  },
  "startTime": {
    "$date": "2014-05-19T20:30:00.000Z"
  },
  "endTime": {
    "$date": "2014-05-19T21:30:00.000Z"
  }
}

How could I query events by specific startTime in like the following:

var startTime = '2014-05-18T20:00:00.000Z';

db.collection.find({startTime: {$eq: startTime}});

My problem is I need ignore time and use only date in the query. How could I construct my query?

Erik
  • 14,060
  • 49
  • 132
  • 218

3 Answers3

9

Define a date at midnight of the day you want to query and at midnight of the following day. Then use the $lt (less-than) and $gt (greater-than) operators to get all results in the timespan between these two points in time.

var from = new Date('2014-05-18T20:00:00.000Z');
var to = new Date('2014-05-19T20:00:00.000Z');

db.collection.find({startTime: {$gt: from, $lt:to}});
Bruno Freitas
  • 321
  • 3
  • 13
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • How could I create endTime based on startTime? – Erik May 26 '14 at 06:34
  • @Erik You could increment the day of a date by using `somedate.setDays(someDate.getDays() + 1)`. And yes, this works across month-boundaries. Trying to set the date to January 32nd sets it to February 1st. More about node.js and date manipulation [in this blog article](http://www.robertprice.co.uk/robblog/2011/05/javascript_date_time_and_node_js-shtml/). – Philipp May 26 '14 at 06:42
  • Thanks! and how could I ignore time? – Erik May 26 '14 at 06:44
  • @Erik You can't. A `Date` object always represents a specific point in time, accurately to the millisecond. A day is a 24 hour time period. A time period is defined by a start-date and an end-date. When you want to select all dates in a given 24 hour time period, you need to query all dates larger than the beginning and less than the end of that time period. – Philipp May 26 '14 at 06:48
0

You need to create Date objects this should help

var start = new Date(2010, 3, 1);
var end = new Date(2010, 4, 1);

http://cookbook.mongodb.org/patterns/date_range/

Abs
  • 3,902
  • 1
  • 31
  • 30
  • But I have only startTime. – Erik May 26 '14 at 06:33
  • 1
    db.collection.find({startTime: {$eq: new Date("2014-05-19T20:30:00.000Z")}}); – Abs May 26 '14 at 06:36
  • 1
    I get the following error: "Can't use $eq with Date." – Erik May 26 '14 at 06:37
  • 1
    try it like this db.collection.find({"createdAt" : new Date("2014-05-19T20:30:00.000Z") }); from here http://stackoverflow.com/questions/8835757/return-query-based-on-date – Abs May 26 '14 at 06:44
0

You can store a number in a field with a inverted date. So, for a date like 2023-06-08 you should store the number 20230608. This way, you can search using the following query:

db.collection.find({ startTime: 20230608 })

This method is much easier than using dates and improves database performance.

Arthur Ronconi
  • 2,290
  • 25
  • 25