查找在Mongo上比较两个DateTime字段

时间:2019-02-06 08:02:02

标签: mongodb

我正在尝试比较同一文档的两个字段,但是我需要在其中一个字段上添加几秒钟。

我的查询是:

db.getCollection('period').find({
    $where: function() {
        return this.dt_stop <= this.dt_start.getTime() + 60000 
    } 
})

但是我遇到下一个错误:

Error: error: {
    "ok" : 0,
    "errmsg" : "TypeError: this.dt_last_start is undefined :\n@:1:15\n",
    "code" : 139,
    "codeName" : "JSInterpreterFailure"
}

我没有在字段中添加第二个。有想法吗?

---解决- 问题是某些文件的this.dt_stop不存在。最终代码:

db.getCollection('period').find(
{
    "$and" : [ 
        {"dt_stop" : {"$exists" : true}},
        {"dt_start" : {"$exists" : true}}, 
        { $where: function() {return this.dt_end.getTime() <= this.dt_begin.getTime()+60000} 
        } 
    ]
}) 

1 个答案:

答案 0 :(得分:1)

如果brew untap weikengchen/caskformula brew update && brew upgrade 是ISODate,并且您正在将其与时间戳(this.dt_start.getTime()+ 60000)进行比较。

尝试

dt_stop
相关问题