Documents in my MongoDB collection are changing status field very frequently. I can observe it using a mongoDB client (Robo 3T).

Now I'd like to monitor this process using mongo shell (or python script):

mongo --host=localhost db --eval "db.getCollection('events').find({status:'ACTIVE'}).count()"

This returns a correct result, but then mongoDB "caches" it and would not return an updated result for another ~ 10 seconds. I need to have an update every 200 ms.

The same query from Robo 3T always returns an updated result, in less than 5ms.

From my observation, when the load is lower, mongo shell count updates are coming on every request. Also the weird part is that once I start executing a count query in Robo 3T, the numbers start moving in mongo shell / python script.

I can't find any cache-like mechanism information in MongoDB documentation. Can I disable it? Why does it work ok from Robo 3T?

  • What is the output of version() and db.version() in your mongo shell? What sort of deployment are you reading from (standalone, replica set, or sharded cluster)? MongoDB doesn't cache query results so there must be something else amiss when your load is higher. Count without a query predicate is estimated, but your example includes {status:'ACTIVE'}. Can you also try the equivalent query: db.getCollection('events').count({status:'ACTIVE'})? The latest Robo3T (currently 1.2) is using a very old MongoDB shell version (3.4.3) so behaviour may vary if using with a much newer server. – Stennie Feb 18 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.