我正在尝试对 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);
}
});
最佳答案
为了使 $text
查询起作用,MongoDB 需要使用文本索引对该字段进行索引。要通过 mongoose 使用创建此索引
fields: {type: [String], text: true}
See here用于文本索引的 MongoDB 文档。
关于javascript - 使用 Mongoose 在 Node JS 中进行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40806871/