Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  129] [ 4]  / answers: 1 / hits: 16280  / 11 Years ago, wed, february 5, 2014, 12:00:00

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.



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



Every document has a created time associated with it.


More From » mongodb

 Answers
14

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.



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



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 )


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.


[#72698] Tuesday, February 4, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
whitney

Total Points: 642
Total Questions: 110
Total Answers: 98

Location: Solomon Islands
Member since Mon, Jun 20, 2022
2 Years ago
;