• This procedure does take one member out of the replica set at a time. However, this procedure will only affect one member of the set at a time rather than all secondaries at the same time. 

• Before version 2.6 Background index creation operations (page 508) become foreground indexing operations on secondary members of replica sets. After 2.6, background index builds replicate as background index builds on the secondaries. 

## Procedure 

Note: If you need to build an index in a sharded cluster , repeat the following procedure for each replica set that provides each shard . 

Stop One Secondary Stop the mongod process on one secondary. Restart the mongod process without the --replSet option and running on a different port. 8 This instance is now in “standalone” mode. 

For example, if your mongod normally runs with on the default port of 27017 with the --replSet option you would use the following invocation: 

mongod --port 47017 

Build the Index Create the new index using the createIndex() in the mongo shell, or comparable method in your driver. This operation will create or rebuild the index on this mongod instance 

For example, to create an ascending index on the username field of the records collection, use the following mongo shell operation: 

db.records.createIndex( { username: 1 } ) 

## See also: 

Create an Index (page 517) and Create a Compound Index (page 518) for more information. 

Restart the Program mongod When the index build completes, start the mongod instance with the --replSet option on its usual port: 

mongod --port 27017 --replSet rs0 

Modify the port number (e.g. 27017 ) or the replica set name (e.g. rs0 ) as needed. 

Allow replication to catch up on this member. 

Build Indexes on all Secondaries Changed in version 2.6: Secondary members can now build indexes in the back- ground (page 523). Previously all index builds on secondaries were in the foreground. 

For each secondary in the set, build an index according to the following steps: 

1. Stop One Secondary (page 522) 

2. Build the Index (page 522) 

3. Restart the Program mongod (page 522) 

