mongodb index(索引)

Mongodb doc:indexes

Create an Index

官方文档例子

db.collection.createIndex( <key and index type specification>, <options> )

options

Optional. A document that contains a set of options that controls the creation of the index. See Options for details.

Parameter Type Description
background Boolean 建索引过程会阻塞其它数据库操作,background可指定以后台方式创建索 引,即增加 "background" 可选参数。 "background" 默认值为false。
unique Boolean 建立的索引是否唯一。指定为true创建唯一索引。默认值为false.
name string 索引的名称。如果未指定,MongoDB的通过连接索引的字段名和排序顺序生成一个索引名称。
dropDups Boolean 在建立唯一索引时是否删除重复记录,指定 true 创建唯一索引。默认值为 false.
sparse Boolean 对文档中不存在的字段数据不启用索引;这个参数需要特别注意,如果设置为true的话,在索引字段中不会查询出不包含对应字段的文档.。默认值为 false.
expireAfterSeconds integer 指定一个以秒为单位的数值,完成 TTL设定,设定集合的生存时间。
weights document 索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重。

dropIndex

db.collection.dropIndex(index)

index string or document

for example

To drop the index catIdx, you can use either the index name:

db.pets.dropIndex( "catIdx" )

Or you can use the index specification document { "cat" : -1 }:

db.pets.dropIndex( { "cat" : -1 } )

compound index

db.collection.createIndex( { orderDate: 1, zipcode: -1 } )

Note
The order of an index is important for supporting sort() operations using the index.

index types

单索引 Single Field

For a single-field index and sort operations, the sort order (i.e. ascending or descending) of the index key does not matter because MongoDB can traverse the index in either direction.

聚合索引 Compound Index

if a compound index consists of { userid: 1, score: -1 }, the index sorts first by userid and then, within each userid value, sorts by score

Multikey Index

If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays. MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type

Text Indexes

注意:A collection can have at most one text index!
用于全文搜索

索引的使用

Covered Queries 覆盖查询

query criteria and the projection of a query include only the indexed fields, MongoDB will return results directly from the index without scanning any documents or bringing documents into memory

实例

Index Intersection

MongoDB can use the intersection of multiple indexes to fulfill queries. [1] In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.

{ qty: 1 }
{ item: 1 }

MongoDB can use the intersection of the two indexes to support the following query:

db.orders.find( { item: "abc123", qty: { $gt: 15 } } )

Index Prefix Intersection

use an intersection of either the entire index or the index prefix.An index prefix is a subset of a compound index, consisting of one or more keys starting from the beginning of the index.(注意必须是从头开始)

{ qty: 1 }
{ status: 1, ord_date: -1 }

MongoDB can use the intersection of the two indexes:

db.orders.find( { qty: { $gt: 10 } , status: "A" } )

but not:

db.orders.find( { qty: { $gt: 10 } , ord_date: "A" } )

Index Intersection and Compound Indexes

Index intersection does not eliminate(消除) the need for creating compound indexes. However, because both the list order (i.e. the order in which the keys are listed in the index) and the sort order (i.e. ascending or descending), matter in compound indexes, a compound index may not support a query condition that does not include the index prefix keys or that specifies a different sort order(i.e. ascending or descending).

需要自己想想符合索引的排序~~
for example

{ status: 1, ord_date: -1 }

it works

db.orders.find(
   {
     ord_date: { $gt: new Date("2014-02-01") },
     status: {$in:[ "P", "A" ] }
   }
)

if the collection has two separate indexes:

{ status: 1 }
{ ord_date: -1 }

The two indexes can, either individually or through index intersection, support all four aforementioned queries.

Index Intersection and Sort

Index intersection does not apply when the sort() operation requires an index completely separate from the query predicate.
查找的条件和排序的条件完全不同

原文地址:https://www.cnblogs.com/jcuan/p/5698386.html