CURDATE() 函数类似于 mysql for mongoDB

CURDATE() function like in mysql for mongoDB

看到new Date()会显示当前日期和时间。但我只需要日期部分。

 new Date()
ISODate("2015-06-11T06:49:17.684Z")

我只需要日期部分--> ISODate("2015-06-11T00:00:00.000Z")

示例数据如下:

{
  "_id": ObjectId("55743942789a9abe7f4af40e"),
  "msisdn": "9xxxxxxxxx",
  "act_date": ISODate("2014-09-16T00:00:00Z"),
  "date": ISODate("2015-06-07T00:00:00Z"),
  "recharge": {
    "recharge_amt": 100,
    "rechargetype": "WEB"
  },
  "voice": {
    "local_og_mou": 4,
    "local_other_mobile_og_mou": 18,
    "nld_og_mou": 10,
    "nld_other_mobile_og_mou": 3
  },
  "gprs_usage": {
    "total_access_count": 1,
    "total_datavolume_mb": 63
  },
  "sms": {
    "freesms": 0,
    "local_sms_count": 0,
    "nat_sms_count": 2,
    "inter_sms_count": 0
  }
}

需要根据日期字段获取当天数据。 mysql 中是否有任何 CURDATE() 函数用于 mongoDB?

你可以 "round" 将日期对象设为当前日期:

new Date( new Date().valueOf() - new Date().valueOf() % ( 1000 * 60 * 60 * 24 ) )

当然,"round" 约会有多种不同的语言依赖方式,但这是一种 JavaScript 方式。

只是为了更稳健一点,我会使用 范围查询 而不是精确匹配来查询日期(想象一些客户端使用 [=例如 11=]):

// @user3561036 code:
var theDate = new Date( new Date().valueOf() - new Date().valueOf() % ( 1000 * 60 * 60 * 24 )

// One day later:
var theDayAfter = new Date(theDate.valueOf() + 1000 * 60 * 60 * 24)


// Search for date in the range `[theDate, theDayAfter)`
db.colection.find({"date": { $gte: theDate, $lt: theDayAfter } })

您可以采用的一种方法是使用 momentjs library, in particular the .startOf('day') 方法:

var currentDate = moment().startOf('day').toDate();

db.colection.find({"date": currentDate})