在 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
使得今天的日期介于 dateStart
和 dateEnd
之间?
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