本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
索引属性:名称
支持的索引类型
| Option | 3.6 | 4.0 | 5.0 | 8.0 | 弹性集群 |
|---|---|---|---|---|---|
| 单字段 | 支持 | 是 | 是 | 是 | 是 |
| 复合排序 | 支持 | 是 | 是 | 是 | 是 |
| 多键 | 支持 | 是 | 是 | 是 | 是 |
| text | 否 | 否 | 是 | 是 | 否 |
| 地理空间 | 支持 | 是 | 是 | 是 | 是 |
| 向量 | 否 | 否 | 是 | 是 | 否 |
使用 name 选项为索引提供可选名称。
所有示例都使用以下示例文档:
{ "productId": "PROD133726", "sku": "SKU24224", "name": "Basic Printer", "manufacturer": "The Manufacturer", "tags": [ "printer", "basic", "electronics", "business" ], "barcodes": [ "542364671", "886330670", "437445606" ], "reviews": [ { "review_date": ISODate('2024-01-19T21:37:10.585Z'), ... } ], "material": "Polycarbonate", "color": "Space Gray", "supplier": { "supplierId": "SUP4", "location": { "type": "Point", "coordinates": [ -71.0589, 42.3601 ] } }, "productEmbedding": [ -0.019320633663838058, 0.019672111388113596 ], "lastUpdated": ISODate('2025-10-20T21:37:10.585Z') }
单字段
db.collection.createIndex( { "productId": 1 }, { "name": "single_field_index" } )
复合排序
db.collection.createIndex( { "productId": 1, "manufacturer": 1 }, { "name": "compound_index" } )
Multi-key
db.collection.createIndex( { "tags": 1 }, { "name": "multikey_index" } )
文本
db.collection.createIndex( { "name": "text" }, { "name": "text_index" } )
地理空间
db.collection.createIndex( { "supplier.location": "2dsphere" }, { "name": "geospatial_index" } )
向量
db.runCommand({ "createIndexes": "collection", "indexes": [{ "key": { "productEmbedding": "vector" }, "name": "hnsw_index", "vectorOptions": { "type": "hnsw", "dimensions": 2, "similarity": "euclidean", "m": 16, "efConstruction": 64 } }] })