Reposted from old site – original date: Sunday 25 November 2012
Right, so you have a huge dataset in MongoDB in production and your replicaSet is purring away serving all your data. What fun, when suddenly, someone decides that we need to add another index to the database. Quickly, you think, we have 2 options:
1. Lock up the database and do a fast foreground index create and hope we don’t lose too much money on failed/queued transactions OR
2. Do a really slow background index on the data and hope for the best with respect to new documents coming in.
Either of these scenarios is not ideal in any mans language, so there is, of course, a trick…
Remember, you are running a replicaSet? Yeah, so what does a replicaSet do? Replicates, yeah! OK, so we can use that to our advantage!
Take a single node [b]out[/b] of the replicaSet and then build a nice foreground index on it. Then insert the node back into the replicaSet and let it replicate across all the nodes. Simple, yet effective. No chewing up all your resources, waiting for hours or locking any databases!