在 mongodb 中查找今天日期范围内的文档 [英] find document in today's date range in mongodb

查看:62
本文介绍了在 mongodb 中查找今天日期范围内的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合,计划.我想找到一个月范围内的所有内容.以下是数据可能是什么样子的示例:

I have a collection, Plans. I want to find everything within the range of one month. Here is a sample of what the data could look like:

{
  "_id": "someid",
  "dateStart": ISODate("2015-03-01T00:00:00Z"),
  "dateEnd": ISODate("2015-03-31T00:00:00Z"),
  "items": [
    "someotherid"
  ]
}

如何找到 Plan 使得今天的日期介于 dateStartdateEnd 之间?

How can I find the Plan such that today's date is between dateStart and dateEnd?

推荐答案

date1 = new Date('3/1/15');
date2 = new Date('4/1/15');

Plans.find({
  startDate: {$gte: date1},
  endDate: {$lt: date2}
});

新的 Date 对象初始化为 00:00:000,因此您可以在这里充分利用它.另请注意,小于 4/1"相当于小于或等于 3/31 的午夜" - 但前者更容易编写.

New Date objects initialize to 00:00:000 so you can use that to your advantage here. Also note that "less than 4/1" is equivalent to "less than or equal to midnight on 3/31" - but the former is much easier to write.

另请注意,moment 是用于执行此类操作的超级方便实用程序.例如:moment().endOf('month').toDate()

Also note that moment is a super handy utility for doing manipulations like this. For example: moment().endOf('month').toDate()

要添加软件包,只需执行以下操作:

To add the package just do:

$meteor add momentjs:moment

这篇关于在 mongodb 中查找今天日期范围内的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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