MongoDB使用时间戳进行排序 [英] MongoDB using timestamps to sort

查看:753
本文介绍了MongoDB使用时间戳进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mongodb将存储访客数据。我需要删除十分钟后不活动的数据,并将通过cron运行一个命令。

I have a mongodb that will be storing visitor data. I need to delete the data after ten minutes of not being active and will run a command through a cron. How would I do this?

目前集合的设置如下:

{ "_id" : ObjectId("4fd33e0b0feeda3b2406f6be"), "name" : "Dugley Reanimator", "updated" : "Some form of timestmap" }

我应该如何存储一个时间戳,我用IE搜索MySql版本的集合:

How should I go about storing a timestamp that I search the collection with I.E for my MySql version:

$sql = mysql_query('DELETE FROM `visitors` WHERE NOW() > DATE_ADD(`last_seen`, INTERVAL 10 MINUTE)');


推荐答案

您的驱动程序将使用MongoDate时间到PHP中更多的本地表示)。

然后,您可以使用类似以下mongo语句的查询:

Your driver will use a MongoDate time (this may map to a more native representation in PHP).
You can then query using something like the following mongo statement:

db.myCollection.find({updated : { $lte : new ISODate("2012-06-09T16:22:50Z") } })

PHP的粗略翻译是:

A rough translation for PHP would be:

$search = array(
  'updated' => array(
    '$lte' => new MongoDate($tenMinutesAgo))
);
$collection->find($search)

$tenMinutesAgo = new DateTime();
$tenMinutesAgo->modify('-10 minutes');

$search = array('updated' => array('$lte' => $tenMinutesAgo));
$collection->find($search)

这篇关于MongoDB使用时间戳进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆