| 语法 | db.collection.createIndex({ field: 1 }, options) |
|---|---|
| 方向 | 1 升序 / -1 降序 / "text" / "hashed" / "2dsphere" |
| 常用选项 | name / unique / sparse / partialFilterExpression / expireAfterSeconds / collation |
示例
示例
db.orders.createIndex({ status: 1, ts: -1 }, { name: "status_ts" })
db.orders.createIndex({ customerId: 1 }, { unique: true })索引构建进度可用 db.currentOp() 观察;构建完成后用 explain 验证查询是否真的走上索引。