Creates an index on the field specified, if that index does not
already exist.
| Parameters: |
- keys (document) –
For ascending/descending indexes, a document that contains pairs with the
name of the field or fields to index and order of the index. A
1 specifies ascending and a -1 specifies descending.
MongoDB supports several different index types including
text, geospatial, and hashed indexes.
- options (document) – A document that controls the creation of the index. This
argument is optional.
|
Warning
Index names, including their full namespace
(i.e. database.collection) cannot be longer than 128
characters. See the db.collection.getIndexes() field
name for the names of existing indexes.
Example
Create an Ascending Index on a Single Field
The following example creates an ascending index on the field
orderDate.
db.collection.ensureIndex( { orderDate: 1 } )
If the keys document specifies more than one field, then
db.collection.ensureIndex() creates a compound
index.
Example
Create an Index on a Multiple Fields
The following example creates a compound index on the
orderDate field (in ascending order) and the zipcode
field (in descending order.)
db.collection.ensureIndex( { orderDate: 1, zipcode: -1 } )
A compound index cannot include a hashed index
component.
Note
The order of an index is important for supporting
cursor.sort() operations using the index.
See also
- The Indexes section of this manual for full
documentation of indexes and indexing in MongoDB.
- The Create a text Index section for more information and
examples on creating text indexes.
ensureIndex() provides the following options:
| Option |
Value |
Default |
Index Type |
| background |
true or false |
false |
All |
| unique |
true or false |
false |
Ascending/Descending |
| name |
string |
none |
All |
| dropDups |
true or false |
false |
Scalar |
| sparse |
true or false |
false |
Ascending/Descending |
| expireAfterSeconds |
integer |
none |
TTL |
| v |
index version |
The default index version depends on the version of
mongod running when creating the index. Before version
2.0, the this value was 0; versions 2.0 and later use version 1. |
All |
| weights |
document |
1 |
Text |
| default_language |
string |
english |
Text |
| language_override |
string |
“language” |
Text |
| Options: |
- background (boolean) – Specify true to build the index in the
background so that building an index will not
block other database activities.
- unique (boolean) – Specify true to create a unique index so that
the collection will not accept insertion of
documents where the index key or keys matches an
existing value in the index.
- name (string) – Specify the name of the index. If unspecified,
MongoDB will generate an index name by concatenating
the names of the indexed fields and the sort order.
- dropDups (boolean) – Specify true when creating a unique index, on a
field that may have duplicate to index only the
first occurrence of a key, and remove all
documents from the collection that contain
subsequent occurrences of that key.
- sparse (boolean) – If true, the index only references documents
with the specified field. These indexes use less
space, but behave differently in some situations
(particularly sorts.)
- expireAfterSeconds (integer) – Specify a value, in seconds, as a TTL to
control how long MongoDB will retain documents in
this collection. See Expire Data from Collections by Setting TTL
for more information on this functionality.
- v – Only specify a different index version in unusual
situations. The latest index version (version 1) provides a
smaller and faster index format.
- weights (document) –
For text index only. The document contains
field and weight pairs. The weight is a number
ranging from 1 to 99,999.
The weight of the index field denote the
significance of the field relative to the other
indexed fields in terms of the score. You can
specify weights for some or all the indexed fields.
See Control Results of Text Search with Weights
to adjust the scores.
- default_language (string) –
For text index only. Specify the language that
determines the list of stop words and the rules for
the stemmer and tokenizer. The default language for
the indexed data is english.
See Text Search Languages for the available
languages and
Specify a Language for Text Index for
more information and example.
- language_override (string) –
For text index only.
Specify the name of the field in the document that
contains, for that document, the language to override
the default language.
See
Create a text Index on a Multi-language Collection.
|
Please be aware of the following behaviors of
ensureIndex():
To add or change index options you must drop the index using the
dropIndex() method and issue another
ensureIndex() operation
with the new options.
If you create an index with one set of options, and then issue
the ensureIndex() method
with the same index fields and different options without
first dropping the index,
ensureIndex() will not rebuild the existing
index with the new options.
If you call multiple ensureIndex()
methods with the same index specification at the same time, only
the first operation will succeed, all other operations will have
no effect.
Non-background indexing operations will block all other
operations on a database.
See also
In addition to the ascending/descending indexes, MongoDB provides
the following index types to provide additional functionalities: