Build the Index on the Primary To build an index on the primary you can either: 

1. Build the index in the background (page 523) on the primary. 

2. Step down the primary using the rs.stepDown() method in the mongo shell to cause the current primary to become a secondary graceful and allow the set to elect another member as primary. 

Then repeat the index building procedure, listed below, to build the index on the primary: 

(a) Stop One Secondary (page 522) 

(b) Build the Index (page 522) 

(c) Restart the Program mongod (page 522) 

Building the index on the background, takes longer than the foreground index build and results in a less compact index structure. Additionally, the background index build may impact write performance on the primary. However, building the index in the background allows the set to be continuously up for write operations while MongoDB builds the index. 

## Build Indexes in the Background 

By default, MongoDB builds indexes in the foreground, which prevents all read and write operations to the database while the index builds. Also, no operation that requires a read or write lock on all databases (e.g. listDatabases ) can occur during a foreground index build. 

Background index construction (page 508) allows read and write operations to continue while building the index. 

## See also: 

Index Concepts (page 486) and Indexing Tutorials (page 517) for more information. 

## Considerations 

Background index builds take longer to complete and result in an index that is initially larger, or less compact, than an index built in the foreground. Over time, the compactness of indexes built in the background will approach foreground- built indexes. 

After MongoDB finishes building the index, background-built indexes are functionally identical to any other index. 

## Procedure 

To create an index in the background, add the background argument to the createIndex() operation, as in the following index: 

db.collection.createIndex( { a: 1 }, { background: true } ) 

Consider the section on background index construction (page 508) for more information about these indexes and their implications. 

## Build Old Style Indexes 

Important: Use this procedure only if you must have indexes that are compatible with a version of MongoDB earlier than 2.0. 

MongoDB version 2.0 introduced the {v:1} index format. MongoDB versions 2.0 and later support both the {v:1} format and the earlier {v:0} format. 

