简体   繁体   中英

Filtering records in MongoDB using a date value

In a MongoDB collection i have a record as follows :

{ "_id" : ObjectId("4d0d3945e69a56cf504375b7"), "action" : "Click", "dt" : "Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)","url":"http://www.google.com"}

If i search using db.mycollection.find({url:'http://www.google.com'}) ,the record shows up , but if i search by the date parameter using db.mycollection.find({dt:'Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)'}) the record does not show up .

What is wrong about command db.mycollection.find({dt:'Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)'}) ?

Please Help Thank You

It does work :

> db.test.save({ "_id" : ObjectId("4d0d3945e69a56cf504375b7"), "action" : "Click", "dt" : "Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)","url":"http://www.google.com"})
> db.test.find({dt:'Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)'})
{ "_id" : ObjectId("4d0d3945e69a56cf504375b7"), "action" : "Click", "dt" : "Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)", "url" : "http://www.google.com" }

Generally you'll want to save dates as dates rather than strings though.

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