My case is that I run several queries in Mongo and measure the response time. I know that Mongo uses Memory Mapped files and cache some data in RAM.

If I create an index and run a query the execution time is 2,07 seconds. When I run the qeury again the execution time is 0,017 seconds.

This means that the file is mapped into memory by the OS on the first access and the OS will keep it in cache until it decides to page it out.

Because I want to make some benchmarks I want somehow to remove the mapped results from memory.

free -g:

               total       used       free     shared    buffers     cached

Mem:            29          29          0          0          0         29
-/+ buffers/cache:          0         29
Swap:            0          0          0

I try db.collection.getPlanCache().clear() but it doesnt work.

Then I try:

sudo service mongod stop

I reboot my machine

sync; echo 3 > /proc/sys/vm/drop_caches
sudo service mongod start

free -g:

               total       used       free     shared    buffers     cached

Mem:            29          0          0          0          0         0
-/+ buffers/cache:          0         29
Swap:            0          0          0

In above it seems that cache is free now. But when I execute again the query the execution time is 0,017 seconds. It seems that mongo has manage to cache or preserve the data somewhere. The desired execution time would be 2,07 seconds as I clear the cache.

Can anyone tell me how I can actually delete the cached results in Mongo?

  • ,I think you don't have planCacheWrite authorization access of that user. May 31, 2018 at 9:37
  • how can i ceck this?
    – e7lT2P
    May 31, 2018 at 9:37
  • Could you confirm through "db.getRoles()" to that user , against to that database. May 31, 2018 at 9:39
  • when i run on primary db,getRoles() i get [ ]
    – e7lT2P
    May 31, 2018 at 9:40
  • That means you don't have any particular role assigned to that database collections. May 31, 2018 at 9:43

2 Answers 2


This will give your all possible option to play with MongoDb Cache.


PlanCache help

`db.referenceDataPoints.getPlanCache().help()` - show PlanCache help
`db.referenceDataPoints.getPlanCache().listQueryShapes()` - displays all query shapes in a collection
`db.referenceDataPoints.getPlanCache().clear()` - drops all cached queries in a collection
`db.referenceDataPoints.getPlanCache().clearPlansByQuery(query[, projection, sort, collation])` - drops query shape from plan cache
`db.referenceDataPoints.getPlanCache().getPlansByQuery(query[, projection, sort, collation])` - displays the cached plans for a query shape

As per MongoDB documentation here PlanCache.clear() removes all cached query plans for a collection.

The method is only available from the plan cache object of a specific collection; i.e.


Required Access

On systems running with authorization, a user must have access that includes the planCacheWrite action.

For your further refernce Manage Users and Roles in MongoDB and Collection-Level Access Control in MongoDB

  • ok thank you I will try this and I will inform if it goes well
    – e7lT2P
    May 31, 2018 at 9:48
  • In mongod.conf I have set authorization:"disabled". This means that I don't need to create a role?
    – e7lT2P
    May 31, 2018 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.