Mongoose 以 ISO
格式存储时间,如下所示:
{
"_id": {
"$oid": "5b6ed55b6a12624b1853b29a"
},
"time": {
"$date": "2018-08-11T12:23:55.627Z"
},
"location": "Kathmandu",
"temperature": 23,
"description": "moderate rain",
"humidity": 88,
"__v": 0
}
WeatherSchema.countDocuments({
'location': city,
'time': {
$lt: Date.now() - 1.8e+6
}
但是 Date.now()
给出的时间戳为 毫秒
,并且 数据库
的时间为 ISO 格式
。
最佳答案
您需要使用另一种方式来添加 30 分钟,例如
var date = new Date();
var modifiedDate = new Date();
modifiedDate.setMinutes(date.getMinutes() + 30);
WeatherSchema.countDocuments({
'location': city,
'time': {
$lt: modifiedDate
}
现在,modifiedDate
是一个 Date
对象,因此在查询时, Mongoose 将相应地将其转换为 ISO 格式。
关于node.js - 如何从 Mongodb 数据库中减去 ISOFormat [2018-08-11T12 :23:55. 627Z] 的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51799757/