最近一次对MongoDB进行更新的最快方法是什么 [英] What is the fastest way to see when the last update to MongoDB was made
问题描述
我正在编写一个长轮询脚本来检查mongo集合中的新文档,而我知道检查循环的过去迭代中是否进行过更改的唯一方法是执行查询获取上一个文档的ID,然后解析该ID中的时间戳,并查看它是否大于我上次发出请求以来剩余的时间戳.
I'm writing a long-polling script to check for new documents in my mongo collection and the only way I know of checking to see whether or not changes have been made in the past iteration of my loop is to do a query getting the last document's ID and parsing out the timestamp in that ID and seeing if it's greater than the timestamp left since I last made a request.
是否有某种方法不涉及每次查询或使查询最快?
Is there some kind of approach that doesn't involve making a query every time or something that makes that query the fastest?
我在想这样的查询:
db.chat.find().sort({_id:-1}).limit(1);
但是它将使用PHP驱动程序.
But it would be using the PHP driver.
推荐答案
最快的方法是在timestamp
字段上创建索引.
The fastest way will be creating indexes on timestamp
field.
Creating index:
db.posts.ensureIndex( { timestamp : 1 } )
Optimizes this query:
db.posts.find().sort( { timestamp : -1 } )
findOne
仅给您最后一个时间戳.
findOne
give you only one the last timestamp.
很高兴为您提供帮助.
这篇关于最近一次对MongoDB进行更新的最快方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!