使用日期值过滤MongoDB中的记录 [英] Filtering records in MongoDB using a date value

查看:331
本文介绍了使用日期值过滤MongoDB中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB集合中,我有如下记录:

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

如果我使用db.mycollection.find({url:'http://www.google.com'})搜索,则会显示记录 ,但是如果我使用db.mycollection.find({dt:'Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)'})通过date参数进行搜索,则记录不会显示. /p>

命令db.mycollection.find({dt:'Sun Dec 19 2010 03:44:21 GMT+0000 (UTC)'})有什么问题?

请帮助 谢谢

解决方案

它确实有效:

> 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" }

通常,您通常希望将日期保存为日期而不是字符串.

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.

这篇关于使用日期值过滤MongoDB中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆