使用
创建全文索引
创建全文索引需使用接口SdbCollection.createIndex(<name>,<indexDef>,[options])。使用格式如下:
createIndex(<索引名>, { <字段1>: "text", [<字段2>: "text"...] });
全文索引可以指定一个或多个字段,普通索引的其它选项(如 Unique, NotNull...)均对全文索引无效,无需指定。例如,在 sample.employee 集合上为 name 及 address 字段上创建复合全文索引,使用语句如下:
db.sample.employee.createIndex('fulltext_idx', { 'name': 'text', 'address': 'text' })
Note:
- 只有字符串类型的字段会被索引,非字符串字段会被忽略。
- 使用全文索引时,不要编辑文档自动生成的
_id
字段及其唯一索引$id
。如果