Filtering records in MongoDB using a date value

蹲街弑〆低调 提交于 2019-12-08 09:27:26

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6898039/filtering-records-in-mongodb-using-a-date-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!