Indexes can usually greatly improve the efficiency of queries. If there is no index, MongoDB must scan each file in the collection and select those records that meet the query criteria when reading data.
The query efficiency of this kind of scanning full collection is very low, especially when dealing with a large amount of data, the query can take dozens of seconds or even minutes, which is very fatal to the performance of the website.
An index is a special data structure, which is stored in a data collection that is easy to traverse and read. An index is a structure that sorts the values of one or more columns in a database table. MongoDB usage Note that before version 3.0.0, the index method is In grammar Parameter Type Description Background Boolean The indexing process blocks other database operations, and background can specify that the index is created in the background, that is, adding the “background” optional parameter. The default value for background is false. Unique Boolean Whether the index established is unique. Specifies that a unique index is created for true. The default value is false. Name String The name of the index. If not specified, MongoDB generates an index name by concatenating the field name and sort order of the index. DropDups Boolean Version 3.0 + is obsolete. Whether to delete duplicate records when creating a unique index, and specify that true create a unique index. The default value is false. Sparse Boolean Indexing is not enabled for field data that does not exist in the document; this parameter requires special attention that, if set to true, documents that do not contain corresponding fields will not be 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 mongod runs when the index is created. Weights Document The index weight value, with a value between 1 and 99999, indicates the score weight of the index relative to other index fields. default_language String For text indexing, this parameter determines the list of rules for deactivated words and stemming and lexical organs. The default is English language_override String For text indexing, this parameter specifies the field name included in the document, the language overrides the default language, and the default value is language. Create an index in the background: Let the creation work be performed in the background by adding the option of background:true when creating the index 3.21.1.
createIndex()
Method ¶
createIndex()
Method to create the index.
db.collection.ensureIndex()
Later versions use the
db.collection.createIndex()
Method,
ensureIndex
()
``can
still
be
used,
but
only
``createIndex()
It’s an alias.Grammar ¶
createIndex()
The basic syntax format of the method is as follows:>db.collection.createIndex(keys, options)
Key
The value is the index field you want to create, 1 is the specified index in ascending order, and if you want to create the index in descending order, specify-1.Example ¶
>db.col.createIndex({"title":1})
>
createIndex()
Method, you can also set to create indexes using multiple fields (called composite indexes in relational databases).>db.col.createIndex({"title":1,"description":-1})
>
createIndex()
Receive optional parameters. The list of optional parameters is as follows:Example ¶
db.values.createIndex({open: 1, close: 1}, {background: true})