Jasch1 Jasch1 - 1 year ago 210
Javascript Question

Full-Text Search in Node JS with Mongoose

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) {
} else {

Answer Source

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

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

See here for the mongodb documenation of text indices.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download