官方文檔例子mongodb
db.collection.createIndex( <key and index type specification>, <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 之間,表示該索引相對於其餘索引字段的得分權重。 |
db.collection.dropIndex(index)
index string or documentapp
To drop the index catIdx, you can use either the index name:ui
db.pets.dropIndex( "catIdx" )
Or you can use the index specification document { "cat" : -1 }:spa
db.pets.dropIndex( { "cat" : -1 } )
db.collection.createIndex( { orderDate: 1, zipcode: -1 } )
Note:
The order of an index is important for supporting sort() operations using the index.code
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.htm
if a compound index consists of
{ userid: 1, score: -1 }
, the index sorts first by userid and then, within each userid value, sorts by scoreblog
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排序
注意:A collection can have at most one text index!
用於全文搜索
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
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 } } )
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 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 does not apply when the sort() operation requires an index completely separate from the query predicate. 查找的條件和排序的條件徹底不一樣