3.21. MongoDB index

发布时间 : 2025-10-25 13:34:22 UTC      

Page Views: 9 views

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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。