MongoDB使用时间戳进行排序

编程入门 行业动态 更新时间:2024-10-27 14:23:25
本文介绍了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使用时间戳进行排序

本文发布于:2023-10-28 08:49:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!