OPTIONS

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 allows read and write operations to continue while building the index.

See also

Index Concepts and Indexing Tutorials 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 ensureIndex() operation, as in the following index:

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

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