1. When you call ensureindex to create an index, you must ensure that no index has been created on the corresponding key before. Otherwise, if the statement is not executed, the index will retain the old version.
Example: we can create a common index for people: DB. People. ensureindex ({"Age": 1 });
Then we can create a unique index for the age key again: DB. People. ensureindex ({"Age": 1 },{ "unique": true })
If the original data contains duplicate age keys, the creation will fail and an error will be reported, but we will not encounter this situation, and the statement will return soon.
2. Options for creating indexes: DB. collectionname. ensureindex (index dictionary, option dictionary)
A. Unique: {"unique": true}. The index value is unique, that is, any two documents are mutually exclusive.
B. Background: {"background": true}. Create an index in the background. It takes a lot of time to create an index, so it is better in the background.
To be continued ......