The index in MongoDB is similar to relational database, it is to improve the efficiency of query and sorting, and the principle of implementation is basically consistent. Because the key (field) in the collection can be a normal data type, it can also be a subdocument. MongoDB can create indexes on various types of keys.
Indexes can often greatly improve the efficiency of queries, and if there is no index, MongoDB must scan every file in the collection and select those records that meet the query criteria when reading the data. This scan full set of query efficiency is very low, especially in the processing of large amounts of data, the query can take dozens of seconds or even a few minutes, which is very fatal to the performance of the site. Indexes are special data structures that are stored in an easy-to-traverse collection of data that is a structure for sorting the values of one or more columns in a database table.
The index of MongoDB is the key field inside the collection collection, and the default will always have a default index _id, which is the only index that cannot be deleted. You can also specify uniqueness for an index yourself manually:
The MongoDB index is divided into 7 categories:
1. Default index (_ID)
MongoDB has a default "_id" key, which is equivalent to the "primary key" role. After the collection is created, the system automatically creates an index on the "_id" key, which is the default index, the index is called "_id_", it cannot be deleted, and the _id value is constant.
2. Single-Key index
The index value is a single value, such as a string, update date, and so on.
3. Multi-Key index
Index values exist for multiple values at the same time, such as arrays.
4. Composite Index
The combination of a single-key index and a multi-built index.
5. Expired Index
An index that expires after a period of time (automatic destruction of records) and is suitable for processing time-limited records such as user login information, such as a session or cookie, expiration of some storage logs, and so on.
The stored field value must be the specified time type Isodate or the isodate array, and the timestamp cannot be used, otherwise the index will be invalidated.
Recording the automatic destruction process is a background program that runs every 60S, and is not a very punctual automatic program.
If you specify a isodate array, the program automatically destroys the oldest record in the array.
An expired index cannot be referenced by a composite index.
6. Full-Text Indexing
Mainly used for search business index, through a variety of different query methods and conditions for MongoDB full-text index, suitable for search engine and site search business.
The MongoDB full-text index currently does not support Chinese.
In practice, MongoDB's full-text index does not support the Chinese index, so it is very efficient and cost-effective to rely solely on MongoDB for full-text indexing to support search operations, and in general the industry is not recommended to use MongoDB for full-text indexing. Unless MongoDB officially increases support for Chinese indexes, MongoDB still has more to do with the role of the Data Warehouse.
7. Location Index
Based on the interval value of the range index query, this index is suitable for the more and more popular taxi software to query the location of the car through the index to improve the efficiency of the query, as well as online shopping for food when the query distance within the restaurant address and so on.
The ultimate goal of the index is to improve query efficiency and speed, the index can not be too many, you need to rationally create the most appropriate.
MongoDB Index Introduction