index
MongoDB is based on a set of index (index), the role of the index is similar to the traditional relational database, currently in order to speed up the search.
MongoDB index B-tree data structure is formed and the corresponding algorithm.
By default, while building the collection, MongoDB database collections _id automatically create a unique index, you can avoid repeating the same record into the document _id values
Single field (key) index
语法: db.collection_name.createIndex({<key>:<n>})
Command Description: indexing a collection of documents of a key, key name is a bond, n = 1 is ascending, n = -1 descending.
The establishment of a single key index
display:
Nested single document index fields
The only index field values
语法: db.collection_name,createIndex({<key>:<n>,<key>:<n>,…},
{unique:true})
Command Description: Create a unique index on one or more fields. Key name is a bond, n = 1 is ascending, n = -1 descending
Multiple-Field Index
语法: db.collection_name.createIndex({<key>:<n>,<key>:<n>,…})
Command Description: index of two or more fields. Key name is a bond, n = 1 is ascending, n = -1 descending
Establish a multi-field index
Multi-column unique index
Text Index
语法: db.collection_name.createIndex({<key1>:”text”,<key2>:”text’,…})
Command Description: In the collection, the establishment of a text index of the document text field content
The basic text index
Weight is assigned text index
Wildcard text index
Hash indexes
ensureIndex () Index
语法: db.collection_name.ensureIndex({{key1:n},{key2:n},…},option)
Command Description: key1, key2 is the collection of keys; n = 1 in ascending order, n = -1 is descending; option is optional
Other index-related methods
db.collection.dropIndex (index): remove the specified collection indexing. Index parameter specifies the need to remove a set of index names, available getIndexes () function to get the names of all indexed collection.
db.collection.dropIndexes (): Removes a collection of all the index names.
db.collection.getIndexes (): Returns a specified set of documents indexed array of conventional description information.
db.collection.reIndex (): delete all indexes on the specified collection, and rebuild all existing indexes. In the case of a large data set, the operation is very resource consuming to run the server.
db.collection.totalIndexSize (): provide the specified collection index size report information.