What is the fastest way to see when the last update to MongoDB was made

别说谁变了你拦得住时间么 提交于 2019-12-11 14:34:58

问题


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?

I was thinking a query like this:

db.chat.find().sort({_id:-1}).limit(1);

But it would be using the PHP driver.


回答1:


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 give you only one the last timestamp.

nice to help you.




回答2:


@Zagorulkin Your suggestion is surely going to help in the required scenario. However i don't think so sort() works with findOne().



来源:https://stackoverflow.com/questions/14332101/what-is-the-fastest-way-to-see-when-the-last-update-to-mongodb-was-made

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!