Mongo date range query (GMT)

statice

I have two variables:

var start = 'Sun, 17 Aug 2014 04:00:00 GMT';
var end = 'Mon, 18 Aug 2014 04:00:00 GMT';

when I try to run a query in mongodb command line :

 db.model.find({"utctime": {$gte: start, $lt: end}})

It does not return any data.

The documents are:

{ "__v" : 0, "_id" : ObjectId("53f1663cb07994e23738d96f"), "utctime" : ISODate("2014-08-18T02:34:21.225Z")}
{ "__v" : 0, "_id" : ObjectId("53f1646ef2ab609325414462"), "utctime" : ISODate("2014-08-17T23:06:21.364Z")}
{ "__v" : 0, "_id" : ObjectId("53f163c6f2ab609325414461"), "utctime" : ISODate("2014-08-17T23:06:21.364Z")}
{ "__v" : 0, "_id" : ObjectId("53f1066a790f5f3d11a42a4b"), "utctime" : ISODate("2014-08-17T19:45:19.669Z")}
{ "__v" : 0, "_id" : ObjectId("53f0d9273af087e674e6f351"), "utctime" : ISODate("2014-08-17T16:31:54.715Z")}
Neil Lunn

These are "date" objects and not strings. Use either the JavaScript Date object constructor or the ISODate helper to create the objects for comparison. Or otherwise the same sort of object cnstructors in your actual implementation language:

var start = new Date( "2014-08-17" );
var end = new Date( "2014-08-18" );

Or where the hours and minutes are important:

var start = new ISODate("2014-08-17T04:00:00.000Z");
var end = new ISODate("2014-08-18T04:00:00.000Z");

Then your queries work fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related