Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  161] [ 6]  / answers: 1 / hits: 15781  / 8 Years ago, fri, november 25, 2016, 12:00:00

I'm trying to perform a full-text search on an array of strings in Mongoose and I am getting this error:



{ [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 }


However, I do have a text index declared on the field on the User Schema and I confirmed that the text index has been created because I am using mLab.
I am trying to perform a full-text search on fields



Here Is My User Schema:



var userSchema = mongoose.Schema({
local: {
firstName: String,
lastName: String,
username: String,
password: String,
fields: {type: [String], index: true}
}
});


Here is My Code for the Full-Text Search:



User.find({$text: {$search: search}}, function (err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
}
});

More From » node.js

 Answers
7

For $text queries to work, MongoDB needs to index the field with a text index. To create this index by mongoose use


fields: {type: [String], text: true}

See here for the MongoDB documentation of text indexes.


[#59920] Wednesday, November 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikoguym

Total Points: 339
Total Questions: 106
Total Answers: 95

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;