Can I change an existing index in MongoDB without dropping it?

Is it possible to change an existing index in MongoDB without dropping it? I do not see anything in the documentation.

I have a unique code in a String field. Collection of ~ 6M documents. This is a set of replicas.

I know that I can remove the index and add a new one. But this is problematic for two reasons:

1) while the index does not exist, some queries will be very slow. 2) adding a new index (in my project) creates a very high load on the database, which noticeably slows down my website.

+7
source share
1 answer

It is impossible to change the index as you described, and if I thought that the result in terms of performance would be similar - how would the database use the half-created / changed index when this operation continued, for example

Instead, I would recommend using a background parameter to create an index on a single node, if this is your configuration, it will take longer, but it will not interfere with normal operation. Once it is completed, you can abandon the old index at your leisure.

However, if you have a set of replicas (recommended), you should be aware that index creation is always (currently) performed in the foreground in the secondary. If you want to avoid loading your minor ones, then you must follow the steps outlined here to accept the member one at a time and build the index needed before reuniting the set:

http://docs.mongodb.org/manual/administration/indexes/#index-building-replica-sets

Update

A secondary index based on secondary features will be available starting with version 2.6 (for more details see release notes ). This will not apply to previous versions, so the above note will be true for versions prior to 2.6.

Finally, as a rule, indexes built in the background will usually be more and less efficient than the built-in indexes in the foreground, so the methodology described above will continue to be used.

+6
source

All Articles