mongodb 中find中执行javascript $where

a. new Date() 和 Date()不是一回事,参考

http://stackoverflow.com/questions/3505693/difference-between-datedatestring-and-new-datedatestring

如果想获得epoch以来的毫秒数,必须用new Date()。

而Date(),似乎就只是一个函数,返回一个字符串而已。没什么大作用,反而容易混淆人的概念。

b. Date对象提供了getTime()

2. 查询语句中,可以用$where operator来执行JavaScript函数, 比如:

1
db.display.find({$and: [{$where: function() {return new Date().getTime() / 1000 - this.last_active_time > 300}}, {status: "offline"}]})
原文地址:https://www.cnblogs.com/HuiLove/p/3981651.html