如何在mongoose中查询今天(从00h:00m:00到现在)`created`的所有数据?

时间:2014-02-15 09:19:15

标签: mongoose

如何在mongoose中查询created今天(从00h:00m:00到现在)的所有数据?

{
  "user": ObjectId("52fe173148ee58a0180c1d77"),
  "caption": "aaaa",
  "created": ISODate("2014-02-14T16:04:57.98Z"),
  "__v": NumberInt(0)
}

{
  "user": ObjectId("52fe173148ee58a0180c1d77"),
  "caption": bbb",
  "created": ISODate("2014-02-14T14:10:27.986Z"),
  "__v": NumberInt(0)
}

1 个答案:

答案 0 :(得分:2)

只需创建一个包含今天开头的Date对象,然后在查询中使用该对象:

var now = new Date();
var startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
MyModel.find({created_on: {$gte: startOfToday}}, function (err, docs) { ... });