如何从Mongodb数据库中减去ISOFormat [2018-08-11T12:23:55.627Z]中的时间?

时间:2018-08-11 12:36:08

标签: node.js mongodb express mongoose openweathermap

ISO格式的猫鼬存储时间如下:

{
    "_id": {
        "$oid": "5b6ed55b6a12624b1853b29a"
    },
    "time": {
        "$date": "2018-08-11T12:23:55.627Z"
    },
    "location": "Kathmandu",
    "temperature": 23,
    "description": "moderate rain",
    "humidity": 88,
    "__v": 0
}
 我想检查数据库在30分钟时间内是否已经有数据。像这样

WeatherSchema.countDocuments({
      'location': city,
      'time': {
        $lt: Date.now() - 1.8e+6
      }

但是Date.now()millisecond中提供了时间戳,而DatabaseISO-Format中具有了时间。

1 个答案:

答案 0 :(得分:0)

您需要使用另一种方法来增加30分钟,例如

var date = new Date();
var modifiedDate = new Date();
modifiedDate.setMinutes(date.getMinutes() + 30);

WeatherSchema.countDocuments({
  'location': city,
  'time': {
   $lt: modifiedDate
}

现在,modifiedDate是一个Date对象,因此在查询时会通过猫鼬将其相应地转换为ISO格式。