Mongodb 简明教程
MongoDB - Indexing
索引支持查询的高效解析。如果没有索引,MongoDB 必须扫描集合的每个文档,以选择与查询语句匹配的那些文档。此扫描效率极低,并且要求 MongoDB 处理大量数据。
Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and require MongoDB to process a large volume of data.
索引是一种特殊数据结构,使用易于遍历的形式存储一小部分数据集。索引按索引中指定的字段值对特定字段的值或多组字段的值进行排序并存储这些值。
Indexes are special data structures, that store a small portion of the data set in an easy-to-traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in the index.
The createIndex() Method
要创建索引,您需要使用 MongoDB 的 createIndex() 方法。
To create an index, you need to use createIndex() method of MongoDB.
Syntax
createIndex() 方法的基本语法如下()。
The basic syntax of createIndex() method is as follows().
>db.COLLECTION_NAME.createIndex({KEY:1})
此处 key 是您希望创建索引的字段的名称,1 表示升序。要以降序创建索引,您需要使用 -1。
Here key is the name of the field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.
Example
>db.mycol.createIndex({"title":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>
在 createIndex() 方法中,您可以传递多个字段,以便在多个字段上创建索引。
In createIndex() method you can pass multiple fields, to create index on multiple fields.
>db.mycol.createIndex({"title":1,"description":-1})
>
此方法还接受选项列表(这些选项是可选的)。以下是列表 −
This method also accepts list of options (which are optional). Following is the list −
Parameter |
Type |
Description |
background |
Boolean |
Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false. |
unique |
Boolean |
Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index. Specify true to create a unique index. The default value is false. |
name |
string |
The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order. |
sparse |
Boolean |
If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false. |
expireAfterSeconds |
integer |
Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection. |
weights |
document |
The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score. |
default_language |
string |
For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is English. |
language_override |
string |
For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language. |
The dropIndex() method
您可以使用 MongoDB 的 dropIndex() 方法删除特定索引。
You can drop a particular index using the dropIndex() method of MongoDB.
Syntax
DropIndex() 方法的基本语法如下()。
The basic syntax of DropIndex() method is as follows().
>db.COLLECTION_NAME.dropIndex({KEY:1})
此处,“key”是希望删除现有索引的文件名称。除了索引规范文档(以上语法)之外,还可以直接指定索引名称,如下所示:
Here, "key" is the name of the file on which you want to remove an existing index. Instead of the index specification document (above syntax), you can also specify the name of the index directly as:
dropIndex("name_of_the_index")
The dropIndexes() method
此方法删除集合中的多个(已指定的)索引。
This method deletes multiple (specified) indexes on a collection.
Syntax
DropIndexes() 方法的基本语法如下():
The basic syntax of DropIndexes() method is as follows() −
>db.COLLECTION_NAME.dropIndexes()
Example
假设我们在命名的 mycol 集合中创建了 2 个索引,如下所示:
Assume we have created 2 indexes in the named mycol collection as shown below −
> db.mycol.createIndex({"title":1,"description":-1})
下面的示例删除上面创建的 mycol 中的索引:
Following example removes the above created indexes of mycol −
>db.mycol.dropIndexes({"title":1,"description":-1})
{ "nIndexesWas" : 2, "ok" : 1 }
>
The getIndexes() method
此方法返回集合中所有索引的描述。
This method returns the description of all the indexes int the collection.
Syntax
以下是 getIndexes() 方法的基本语法:
Following is the basic syntax od the getIndexes() method −
db.COLLECTION_NAME.getIndexes()
Example
假设我们在命名的 mycol 集合中创建了 2 个索引,如下所示:
Assume we have created 2 indexes in the named mycol collection as shown below −
> db.mycol.createIndex({"title":1,"description":-1})
下面的示例检索集合 mycol 中的所有索引:
Following example retrieves all the indexes in the collection mycol −
> db.mycol.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.mycol"
},
{
"v" : 2,
"key" : {
"title" : 1,
"description" : -1
},
"name" : "title_1_description_-1",
"ns" : "test.mycol"
}
]
>