简体   繁体   中英

How to construct date range query?

My mongo document is as below

{{

"startTime" : "2012-12-06T18:30:00.000Z",
"endTime" : "2012-12-07T18:30:00.000Z",
"cost" : "40",
"_id" : ObjectId("50c31b65d2dfa2271b000001")
}
{

"startTime" : "2012-12-07T18:30:00.000Z",
"endTime" : "2012-12-08T18:30:00.000Z",
"cost" : "40",
"_id" : ObjectId("50c31b65d2dfa2271b000001")
}
{

"startTime" : "2012-12-05T18:30:00.000Z",
"endTime" : "2012-12-07T18:30:00.000Z",
"cost" : "40",
"_id" : ObjectId("50c31b65d2dfa2271b000001")
}
}

I have tried to query the mongo document based on the date range, my query is as follows:

db.collection.find({"startTime":{"$gte":"2012-12-05T18:30:00.000Z"},"endTime":  {"$lt":"2012-12-12T18:30:00.000Z"}});

Response is null...

如下更改查询,它将返回预期的结果。

db.collection.find({"startTime":{"$gte":ISODate("2012-12-05T18:30:00.000Z")},"endTime":  {"$lt":ISODate("2012-12-12T18:30:00.000Z")}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM