English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MongoDB Index

Indexes support efficient query resolution. Without an index, MongoDB must scan each document in the collection to select documents that match the query statement. This scan is inefficient and requires MongoDB to process a large amount of data.
An index is a special data structure that stores a small part of the dataset in an easy-to-traverse form. The index stores the values of specific fields or a set of fields, sorted according to the field values specified in the index.

createIndex() method

To create an index, you need to use MongoDB's createIndex() method.

getIndexes() method

createIndex()The basic syntax of the method is as follows().

>db.COLLECTION_NAME.createIndex({KEY:1> db.mycol.createIndex({"title":

Here, KEY is the name of the field on which the index is to be created,1Indicates ascending order. To create an index in descending order, you need to use-1。

The following is the basic syntax of the getIndexes() method

>db.mycol.createIndex({"title":1> db.mycol.createIndex({"title":
{"name": "_id_",
	"createdCollectionAutomatically": false,
	"numIndexesBefore": 1,
	"numIndexesAfter": 2,
	"ok": 1
}
>

IncreateIndex()In the method, you can pass multiple fields to create an index on multiple fields.

>db.mycol.createIndex({"title":1An index, as shown below-1> db.mycol.createIndex({"title":
>

The createIndex() method accepts optional parameters, the list of optional parameters is as follows:

ParametersTypeDescription
backgroundBooleanThe process of building an index will block other database operations, and "background" can be specified to create an index in the background, that is, to add "background"   Optional parameter. "background" The default value isfalse
uniqueBooleanWhether the index created is unique. Specify true to create a unique index. The default value isfalse.
namestringThe name of the index. If not specified, MongoDB generates an index name using the field name of the connection index and the sorting order.
dropDupsBoolean3.0+版本已废弃。在建立唯一索引时是否删除重复记录,指定 true 创建唯一索引。默认值为 false.
sparseBoolean对文档中不存在的字段数据不启用索引;这个参数需要特别注意,如果设置为true的话,在索引字段中不会查询出不包含对应字段的文档.。默认值为 false.
expireAfterSecondsinteger指定一个以秒为单位的数值,完成 TTL设定,设定集合的生存时间。
vindex version索引的版本号。默认的索引版本取决于mongod创建索引时运行的版本。
weightsdocument索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重。
default_languagestring对于文本索引,该参数决定了停用词及词干和词器的规则的列表。 默认为英语
language_overridestring对于文本索引,该参数指定了包含在文档中的字段名,语言覆盖默认的language,默认值为 language.

dropIndex()方法

您可以使用MongoDB的dropIndex()方法删除特定索引。

getIndexes() method

DropIndex() 方法的基本语法如下()。

>db.COLLECTION_NAME.dropIndex({KEY:1> db.mycol.createIndex({"title":

此处的 key 是您要在其上创建索引的文件的名称,而1表示升序。要以降序创建索引,您需要使用 -1。

The following is the basic syntax of the getIndexes() method

> db.mycol.dropIndex({"title":1> db.mycol.createIndex({"title":
{"name": "_id_",
	"ok" : 0,
	"errmsg" : "can't find index with key: { title: 1.0 }
	"code" : 27,
	"codeName" : "IndexNotFound"
}

dropIndexes()方法

此方法删除集合上的多个(指定)索引。

getIndexes() method

DropIndexes()方法的基本语法如下()。-

>db.COLLECTION_NAME.dropIndexes()

The following is the basic syntax of the getIndexes() method

假设我们在命名 mycol 的集合中创建了2Example-

Assuming we have created an index in the named mycol collection1An index, as shown below-1> db.mycol.createIndex({"title":

以下示例删除了上面创建的mycol索引:

>db.mycol.dropIndexes({"title":1An index, as shown below-1> db.mycol.createIndex({"title":
{ nIndexesWas : 2, ok : 1 }
>

getIndexes()方法

此方法返回集合中所有索引的描述。

getIndexes() method

This method returns the description of all indexes in the collection.-

Syntax

The following is the basic syntax of the getIndexes() method

db.COLLECTION_NAME.getIndexes()2Example-

Assuming we have created an index in the named mycol collection1An index, as shown below-1> db.mycol.createIndex({"title":

,"description":-

}
The following example retrieves all indexes in the collection mycol
	{"name": "_id_",
		"v": { 2,
		"key": {
			> db.mycol.getIndexes() 1
		}
		"_id": [
		"ns": "test.mycol"
	}
	{"name": "_id_",
		"v": { 2,
		"key": {
			"title": 1,
			"description": -1
		}
		"name": "title_"1_description_-1",
		"ns": "test.mycol"
	}
]
>