MongoDB中的日期比较 [英] Date comparison in mongodb

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

问题描述

我想检索特定日期之后的所有文档. 我的数据库的日期为-DateAdded:"2014-12-17 10:03:46.000Z"

I want to retrieve all the documents after a particular date. My database has date as - DateAdded:"2014-12-17 10:03:46.000Z"

我写了以下查询-

db.collection.find({DateAdded:{"$lte":new Date("2015-06-17 10:03:46.000Z")}})

但是,即使有日期直到2015-06-24的记录,结果也不会获取任何记录.

But the results doesn't fetches any record even though there are records for the dates upto 2015-06-24.

推荐答案

您可以使用 ISODate 比较日期:

"$lte" : ISODate("2015-06-17T10:03:46Z")

ISODate 之所以有效,是因为这是您的日期所在的格式.

ISODate works because that is the format your date is in.

new Date() 

在ISODate帮助器中包装日期,但无法在查询中进行转换.

wraps the date in an ISODate helper, but is not able to convert in your query.

查看此链接以获取更多信息: http://docs.mongodb.org /manual/core/shell-types/

Check out this link for more information: http://docs.mongodb.org/manual/core/shell-types/

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

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