View a markdown version of this page

多金鑰索引 - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

多金鑰索引

對於具有陣列值的欄位,多索引鍵索引可讓您為陣列中的每個元素建立索引鍵。索引陣列會為陣列的每個元素建立索引項目。

當您的應用程式經常查詢或根據陣列中的值篩選文件時,多金鑰索引很有幫助。

支援的索引屬性

選項 3.6 4.0 5.0 8.0 彈性叢集
name
唯一
稀疏 *
partialFilterExpression *
expireAfterSeconds

* sparsepartialFilterExpression選項無法在相同的索引定義中一起使用。如果您嘗試使用這些選項建立索引,它將會失敗,並出現下列錯誤:

Error in specification: cannot mix partialFilterExpression and sparse options

建立多金鑰索引

使用 createIndex()方法來建立多金鑰索引。方法語法為: db.collection.createIndex(<key>, <options>)

key 參數是指定 欄位和索引排序順序的 JSON 文件:

{ "<field>": <1 (ascending)|-1 (descending)> }

options 參數是 JSON 文件,可指定索引的選項:

{ "name": "<name>", "unique": <true | false>, "sparse": <true | false>, "partialFilterExpression": <filter expression>, "expireAfterSeconds": <seconds before expiry> }

下列範例會在categories欄位上建立多重索引鍵,並以名稱 遞增排序book_categories

db.collection.createIndex( { "categories": 1 }, { "name": "book_categories" } )

如需建立多金鑰索引的範例,請參閱索引屬性