| 定义 | createIndex({ "attrs.$**": 1 }) |
|---|---|
| 适用 | 字段名动态但值类型一致的子文档 |
| 限制 | 不用于排序/分片键场景,通配符嵌套有限制 |
示例
示例
db.products.createIndex({ "attrs.$**": 1 })
db.products.find({ "attrs.color": "red" }).explain("executionStats")通配符索引不覆盖数组字段的多键特性与排序需求,复杂查询仍要显式索引。
字段名不固定的子文档可建通配符索引 { "attrs.$**": 1 },为其中任意标量字段提供等值与范围加速。
| 定义 | createIndex({ "attrs.$**": 1 }) |
|---|---|
| 适用 | 字段名动态但值类型一致的子文档 |
| 限制 | 不用于排序/分片键场景,通配符嵌套有限制 |
db.products.createIndex({ "attrs.$**": 1 })
db.products.find({ "attrs.color": "red" }).explain("executionStats")