CURDATE()函数就像在mongoDB的mysql中一样

时间:2015-06-11 07:53:02

标签: javascript mongodb mongodb-query

已经看到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
  }
}

需要根据日期字段获取当前日期数据。 是否有mongoDB的mysql中的CURDATE()函数?

3 个答案:

答案 0 :(得分:2)

您可以将日期对象“舍入”到当天:

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

当然,有不同的语言依赖方式来“围绕”你的日期,但这是一种JavaScript方式。

答案 1 :(得分:0)

为了更健壮一点,我会使用范围查询而不是与查询日期完全匹配(假设某些客户使用$currentDate更新了数据):

// @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 } })

答案 2 :(得分:0)

您可以采用的一种方法是使用 momentjs 库,尤其是.startOf('day')方法:

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

db.colection.find({"date": currentDate}) 
相关问题