假设:您的数据库中有以下带有时间戳的文档。
> db.dinner.find()
{ "_id" : ObjectId("527d4edc6acba77bbdbcdbc5"), "timestamp" : 1383857393558, "dinner" : "strawberries" }
{ "_id" : ObjectId("527d4ee96acba77bbdbcdbc6"), "timestamp" : 1383770993558, "dinner" : "cherries" }
{ "_id" : ObjectId("527d4f116acba77bbdbcdbc7"), "timestamp" : 1383684593558, "dinner" : "blueberries" }
{ "_id" : ObjectId("527d4fa36acba77bbdbcdbc8"), "timestamp" : 1383943793558, "dinner" : "cranberries" }
>
然后,您可以使用以下代码找到 0、3、6、9、... 天前创作的所有文档:
today = new Date();
db.dinner.find().forEach(
function(entry) {
daysAgo = Math.floor((today.getTime() - entry.timestamp) / 1000 / 60 / 60 / 24);
if (daysAgo % 3 == 0) printjson(entry);
}
)