MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDB index


Indices can usually greatly improve the efficiency of queries. Without indexes, MongoDB must scan each file in the collection and select those records that meet the query conditions when reading data.

The query efficiency of scanning the entire collection is very low. Especially when processing 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.

The index is a special data structure. The index is stored in a data collection that is easy to traverse and read. The index is a structure that sorts the values ​​of one or more columns in the database table


ensureIndex() method

MongoDB uses the ensureIndex() method to create indexes.

Syntax

The basic syntax format of 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, and 1 specifies to create the index in ascending order. , if you want to create indexes in descending order, specify -1.

Example

>db.col.ensureIndex({"title":1})
>

In the ensureIndex() method, you can also set up to use multiple fields to create an index (called a composite index in relational databases).

>db.col.ensureIndex({"title":1,"description":-1})
>

ensureIndex() receives optional parameters. The optional parameter list is as follows:

ParameterTypeDescription
backgroundBoolean The index building process will block other database operations. Background can specify the index to be created in the background, that is, add "background" Optional parameters. "background" default value is false.
uniqueBooleanWhether the index created is unique. Specify true to create a unique index. The default value is false.
namestringThe name of the index. If not specified, MongoDB generates an index name by concatenating the index's field names and sort order.
dropDupsBooleanWhether to delete duplicate records when creating a unique index, specify true to create a unique index. The default value is false.
sparseBooleanDoes not enable indexing for field data that does not exist in the document; this Special attention should be paid to the parameters. If set to true, documents that do not contain the corresponding fields will not be queried in the index field. The default value is false.
expireAfterSecondsintegerSpecify a value in seconds to complete the TTL setting , set the survival time of the collection.
vindex versionThe version number of the index. The default index version depends on the version mongod was running when creating the index.
weightsdocumentIndex weight value, the value is between 1 and 99,999, indicating the score weight of the index relative to other index fields.
default_languagestringFor text indexing, this parameter determines the list of stop words and rules for stems and tokenizers. The default is English
language_overridestringFor text indexes, this parameter specifies the field name contained in the document. The language overrides the default language , the default value is language.

Instance

Create index in the background:

db.values.ensureIndex({open: 1, close: 1}, {background: true})

By adding background:true when creating the index option to have the creation work executed in the background

php.cn