如何从 Mongodb 数据库中减去 ISOFormat [2018-08-11T12:23:55.627Z] 中的时间?
How to subtract time in ISOFormat [2018-08-11T12:23:55.627Z] from the Mongodb Database?
Mongoose 以 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
中给出了时间戳,而 Database
在 ISO-Format
中给出了时间戳。
您需要使用其他方式添加 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 格式。
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()
在 millisecond
中给出了时间戳,而 Database
在 ISO-Format
中给出了时间戳。
您需要使用其他方式添加 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 格式。