3.21. MongoDB index

发布时间 :2025-10-25 12:32:59 UTC      

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.

3.21.1. createIndex() Method

MongoDB usage createIndex() Method to create the index.

Note that before version 3.0.0, the index method is 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)

In grammar 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:

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.

Example

Create an index in the background:

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

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.