Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB index


May 17, 2021 MongoDB


Table of contents


MongoDB index

Indexes can often greatly improve the efficiency of queries, and without indexes, MongoDB must scan each file in the collection and pick records that meet the query criteria when reading data.

This type of scanning of a full collection of queries is very inefficient, especially when working with large amounts of data, queries can take tens of seconds or even minutes, which is very lethal to the performance of the site.

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


The ensureIndex() method

MongoDB uses the ensureIndex() method to create an index.

Grammar

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, 1 creates the index in ascending order for the specified, and if you want to create the index in descending order, you can specify -1.

Instance

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

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

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

ensureIndex() receives optional parameters, as follows:

Parameter Type Description
background Boolean The indexing process blocks other database operations, and background specifies a back-up way to create an index, which adds the "background" optional parameter. The default value for "background" is false.
unique Boolean Whether the index established is unique. S pecify to create a unique index for true. The default is false .
name string The name of the index. If not specified, MongoDB generates an index name by connecting the field names and sort order of the index.
dropDups Boolean Whether to delete duplicate records when a unique index is established, specifying true to create a unique index. The default is false .
sparse Boolean The index is not enabled for field data that does not exist in the document; The default is false .
expireAfterSeconds integer Specify a value in seconds, complete the TTL setting, and set the lifetime of the collection.
index version The version number of the index. The default index version depends on the version that mongod ran when the index was created.
weights document The index weight value, which ranges from 1 to 99,999, represents the index's score weight relative to other index fields.
default_language string For text indexes, this parameter determines a list of rules for deactivation of words and word trunks and phrases. English is the default
language_override string For text indexes, this parameter specifies the field name contained in the document, the language overrides the default language, and the default value is language.

Instance

Create an index in the background:

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

Let the creation work be performed in the background by adding the option of background:true when creating an index