MongoDB Index
PS: About half a month of wood has been updated, due to the previous phase of business and the sudden project of the past few days, resulting in a sharp reduction in online time, it is sorry, later estimated will be better.
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
Ensureindex () method
MongoDB uses the Ensureindex () method to create an index.
Grammar
The basic syntax format for the Ensureindex () method is as follows:
> DB . collection_name . Ensureindex ({KEY:1})
The Key value in the syntax is the index field you want to create, 1 for the specified index in ascending order, or 1 if you want to create the index in descending order.
Instance
> DB . MyCol . Ensureindex ({"title":1}) >
In the Ensureindex () method, you can also set up to create indexes using multiple fields (called Composite indexes in relational databases).
> DB . MyCol . Ensureindex ({"title":1,"description":-1 })>
Ensureindex () receives optional parameters, the optional parameter list is as follows:
Parameter |
Type |
Description |
Background |
Boolean |
The indexing process blocks other database operations, and background can specify a later way to create an index, which is to add "background" optional parameters. The default value for "Background" is false. |
Unique |
Boolean |
Whether the established index is unique. Specifies true to create a unique index. The default value is false. |
Name |
String |
The name of the index. If not specified, MongoDB generates an index name by the field name and sort order of the connection index. |
Dropdups |
Boolean |
Whether to delete duplicate records when establishing a unique index, specifying true to create a unique index. The default value is false. |
Sparse |
Boolean |
Do not enable indexing for field data that does not exist in the document; This parameter requires special attention, and if set to true, documents that do not contain corresponding fields are not queried in the index field ... The default value is false. |
Expireafterseconds |
Integer |
Specify a value in seconds, complete the TTL setting, and set the lifetime of the collection. |
V |
Index version |
The version number of the index. The default index version depends on the version that is run when the index is mongod created. |
Weights |
Document |
The index weight value, which is between 1 and 99,999, that represents the score weight of the index relative to the other indexed fields. |
Default_language |
String |
For a text index, this parameter determines the list of rules for the stop word and the stemming and the word changer. Default to English |
Language_override |
String |
For a text index, this parameter specifies the name of the field contained in the document, the language overrides the default language, and the default value is language. |
Instance
To create an index in the background:
DB . Values . Ensureindex ({open:1, close:1 },{background:true})
Make the creation work in the background by adding the background:true option when creating the index.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Novice MongoDB Learning---(vi) MONGODB index