在 MongoDB 中的日期查询之间 [英] Between dates query in MongoDB

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

问题描述

我正在尝试在 MongoDB 中的两个日期之间进行查询,但我没有得到任何回复,但我确实有正确的数据.我认为日期格式可能是错误的.这是正确的吗?

I'm trying query between two dates in MongoDB but I'm not getting anything back but I do have the correct data. I'm thinking that the date format might be wrong. Is it correct?

这是我正在运行的查询

  { '$gte': Sun Feb 01 2015 02:00:00 GMT+0200 (IST),
      '$lt': Tue Mar 03 2015 02:00:00 GMT+0200 (IST) }

我的 JS 查询代码是这样的

and my JS query code looks like this

query.created = {$gte:new Date(this.props.startDate), $lt:new Date(this.props.endDate)};

推荐答案

Mongo 识别 ISODate 类型.它可用于进行常见的与日期相关的查询(例如您正在尝试执行的查询).

Mongo recognizes the ISODate type. It can be used to make common date-related queries (like the one you're trying to do).

所以它应该是这样的:

{ '$gte': ISODate(01-02-2015T02:00:00Z),
  '$lt': ISODate(03-03-2015T02:00:00Z) }

请进一步阅读文档了解更多详情.

Please read further this documentation for more details.

另外,请查看官方在线课程的链接,其中有一个部分可以解决您的问题.

Also, check out this link to the oficial online courses, there is a section where it cover your problem.

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

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