We're running a huge MongoDB with a large index (made of of four fields) which was created ad-hoc and should now be superseded by slimmer one (of the first two fields). But the problem is we can't just delete the old one as the warmup of the new one may take hours (or days!). We have two secondary instances, is it possible to only delete the large index on one of those and have that instance take over when the new index is warmed? Or is there some way to guide Mongo to the new indices?

Edit: on a small test database Mongo rejects the new index after I delete the old one and instead uses half of third index, even though the new one is a perfect fit for the query. How to guide/update the query plan?

1 Answer 1


I tried this on a collection with millions of documents, and hope this is going to work in a multi-billon document collection:

  1. Create the index in the background. This can seriously hurt your .finds. We got timeouts for the duration of the index creation.
  2. Clear the plan cache.
  3. Create a script to scan all your data with hint to warm the index.
  4. Delete the old index.
  5. Clear the plan cache again.
  6. Try to find some data with the new index, and ensure it's used with explain. You must use different keys each time to ensure explain does not cache your query.
  • Possibly Index Filters can be tried in such situations. Index filter determines which indexes the planner evaluates for a query shape. Note that index filters exist for the duration of the server process and do not persist after shutdown.
    – prasad_
    Commented Jan 12 at 11:48

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.