【发布时间】:2017-04-09 23:10:50
【问题描述】:
我正在尝试对 Mongoose 中的字符串数组执行全文搜索,但出现此错误:
{ [MongoError: text index required for $text query]
name: 'MongoError',
message: 'text index required for $text query',
waitedMS: 0,
ok: 0,
errmsg: 'text index required for $text query',
code: 27 }
但是,我确实在用户架构的字段上声明了一个文本索引,并且我确认已创建文本索引,因为我使用的是 mLab。 我正在尝试对字段执行全文搜索
这是我的用户架构:
var userSchema = mongoose.Schema({
local: {
firstName: String,
lastName: String,
username: String,
password: String,
fields: {type: [String], index: true}
}
});
这是我的全文搜索代码:
User.find({$text: {$search: search}}, function (err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
}
});
【问题讨论】:
-
你用的是哪个版本的 mogoose?
-
目前使用 Mongoose 4.7.0
-
你需要在你的字段上创建一个文本索引:example
标签: javascript node.js mongodb mongoose full-text-search