<p><strike><a href="http://cookbook.mongodb.org/patterns/date_range/" rel="noreferrer">Querying for a Date Range (Specific Month or Day)</a></strike>在<strike><a href="http://cookbook.mongodb.org/" rel="noreferrer">MongoDB Cookbook</a></strike>中对这件事有一个很好的解释,但下面是我自己尝试过的东西,似乎很管用。</p>
<pre><code>items.save({
name: "example",
created_at: ISODate("2010-04-30T00:00:00.000Z")
})
items.find({
created_at: {
$gte: ISODate("2010-04-29T00:00:00.000Z"),
$lt: ISODate("2010-05-01T00:00:00.000Z")
}
})
=> { "_id" : ObjectId("4c0791e2b9ec877893f3363b"), "name" : "example", "created_at" : "Sun May 30 2010 00:00:00 GMT+0300 (EEST)" }
</code></pre>
<p>根据我的实验,您需要将日期序列化为MongoDB支持的格式,因为下面给出了不希望看到的搜索结果。</p>
<pre><code>items.save({
name: "example",
created_at: "Sun May 30 18.49:00 +0000 2010"
})
items.find({
created_at: {
$gte:"Mon May 30 18:47:00 +0000 2015",
$lt: "Sun May 30 20:40:36 +0000 2010"
}
})
=> { "_id" : ObjectId("4c079123b9ec877893f33638"), "name" : "example", "created_at" : "Sun May 30 18.49:00 +0000 2010" }
</code></pre>
<p>在第二个例子中没有预期的结果,但仍然有一个得到。这是因为基本的字符串比较已经完成。</p>