1. Brief introduction
The index is to speed up the query.
Assuming there is no index, MONGODB does a table scan when querying, assuming that the collection is very large. This query can be very slow. The keys are generally indexed when the query is created.
Index the Sort field. Suppose that the sort,mongodb of a field that is not indexed will take all of the data into memory to sort, assuming that the collection is too large to be sorted in memory, MongoDB will error.
2. MongoDB CREATE index
Create an index using the Ensureindex command.
> Db.people.ensureIndex ({"username": 1});
The above statement indexes the username key of the People collection.
3. Combined Index
For composite queries or sorting, set up a composite index.
> Db.people.ensureIndex ({"Date":-1, "username": 1});
The index key's 1 or-1, which indicates the order in which the index was created. 1 is ascending,-1 is in reverse order.
Assuming that the index has only one key, the direction does not matter.
4. Indexing of embedded documents
The comments for the blog collection are indexed according to time:
> Db.blog.ensureIndex ({"Comments.date": 1});
5. Unique index
A unique index ensures that each key in the collection is a unique value.
> Db.people.ensureIndex ({"username": 1}, {"Uniqe": true});
When a unique index is created for an existing collection. It is possible that there have been repeated values, which will create an index failure. Dropdups retains the first document, deleting the values that are repeated later.
> Db.people.ensureIndex ({"username": 1}, {"Unique": true, "dropdups": true});
6. View the established index
The index is placed in the System.indexes collection.
> Db.system.indexes.find ();
Ability to view the index key, name. Which collection to belong to.
7. Deleting an index
By the name of the index. Use Dropindexes to remove an index.
The index name is queried by System.indexes.
> db.user.dropIndexes ({"Username_1": 1});
Address: http://blog.csdn.net/yonggang7/article/details/28100855
The index of MongoDB