Mongodb – Is it possible to delete cache in Mongo

cachemongodbmongodb-3.0query-cache

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?

Best Answer

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.

db.collection.getPlanCache().clear()

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