如何检查 MongoDB 中的当前时间戳?
目前我使用的查询如下:
"last_visit": {
"$lt": NumberLong(1373779876)
}
我存储该查询,然后我有一个 cron 作业使其每分钟运行一次。
如果我的一个用户自某个时间戳以来没有登录,我就能知道。
但是生成的 NumberLong 不正确,因为我当前通过 PHP 使用 time() - 14days,生成的 NumberLong 应该是类似以下的结果:
timestampNOW - someTimestamp(我在 PHP 中生成的)
我当前的解决方案保留过去的绝对日期,而不是相对日期,例如 timeRIGHTNOW - 14 天。
我相信我的查询中需要一个 native Mongo 命令...
最佳答案
我不完全确定你要什么,但我认为有两种选择:
a) 您想要使用 php 构建一个查询,向 mongodb 询问过去两周未登录的所有用户
<?php
// two weeks ago. note that time() returns number of seconds, not milliseconds
$date = (time() - (14 * 24 * 60 * 60)) * 1000;
// construct query: { "last_visit" : { $lt : $date } }
$query = array( 'last_visit' => array( '$lt' => $date ));
$cursor = $collection->find($query);
// you can now iterate $cursor
?>
b) 您想要在 javascript 中执行 mongodb 查询(“ native Mongo 命令”)
db.Foo.find({"last_visit" : {$lt : ISODate().getTime()}});
调用 ISODate
将创建一个新的 ISODate
对象来表示当前时间,并且 getTime
将返回自 UNIX 纪元以来的毫秒数。我不知道如何将该查询传递给 PHP mongodb 驱动程序,但它应该很简单。
关于php - 现在 MongoDB 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18094461/