您可以将文本索引添加到Mongoose架构定义中,使您可以
$text在
find查询中使用运算符来搜索文本索引中包含的所有字段。
创建索引以支持文本搜索,例如
name和
profile.something:
var schema = new Schema({ name: String, email: String, profile: { something: String, somethingElse: String }});schema.index({name: 'text', 'profile.something': 'text'});或者,如果要在索引中包括所有字符串字段,请使用
'$**'通配符:
schema.index({'$**': 'text'});这将使您能够执行分页文本搜索查询,例如:
MyModel.find({$text: {$search: searchString}}) .skip(20) .limit(10) .exec(function(err, docs) { ... });有关更多详细信息,请阅读完整的MongoDB Text
Indexes文档。



