MongoDB查询30秒以上的文档 [英] MongoDB query for document older than 30 seconds

查看:90
本文介绍了MongoDB查询30秒以上的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对30秒钟以上的文档进行查询吗?我正在创建一个清理工作程序,将处于特定状态超过30秒的项目标记为失败.

Does anyone have a good approach for a query against a collection for documents that are older than 30 seconds. I'm creating a cleanup worker that marks items as failed after they have been in a specific state for more than 30 seconds.

没关系,但是我为此使用了 mongojs .

Not that it matters, but I'm using mongojs for this one.

每个文档都有一个关联的created时间.

Every document has a created time associated with it.

推荐答案

我们假设您的文档中有一个created_at或类似字段,该字段的插入时间或修改时间对您而言很重要.

We are assuming you have a created_at or similar field in your document that has the time it was inserted or otherwise modified depending on which is important to you.

您可能不希望遍历结果,而是希望查看中的multi选项. 更新 以将更改应用于与查询匹配的所有文档.设置您想过去的时间应该很简单

Rather than iterate over the results you might want to look at the multi option in update to apply your change to all documents that match your query. Setting the time you want to look past should be fairly straightforward

在shell语法中,它应该与驱动程序几乎相同:

In shell syntax, which should be pretty much the same of the driver:

db.collection.update({
     created_at: {$lt: time },
     state: oldstate 
},
{$set: { state: newstate } }, false, true )

第一个false用于upsert,在这种用法中没有任何意义,第二个true标记用于multi文档更新.

The first false being for upserts which does not make any sense in this usage and the second true marking for multi document update.

如果这些文档的确期限很短,并且之后您再也不需要它们了,那么您可以考虑

If the documents are indeed going to be short lived and you have no other need for them afterwards, then you might consider capped collections. You can have a total size or time to live option for these and the natural insertion order favours processing of queued entries.

这篇关于MongoDB查询30秒以上的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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