2

I'm trying to perform a full text search on a database on mLab. However, apparently the text indexes are not being created. Mongoose version is 5.4.16

Schema:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var mySchema = new Schema({
  title: {
    type: String,
    trim: true,
    required: true
  },
  description: {
    type: String,
    trim: true
  }
});

mySchema.index({
  title: "text",
  description: "text",
});

mongoose.model("Model", mySchema);

module.exports = mongoose.model("Model");

Use following to perform the search:

Model.find({
    $text: { $search: query }
  }).exec(function(err, codes) {...}```

I get the following error:

MongoError: text index required for $text query

1 Answers1

3

I don't know why but I had the same problem with Mongoose and Mlab.

Manually I did the following to solve it.

1) Connect to mlab using the mongo shell

mongo ds245387.mlab.com:45387/<dbname> -u <dbuser> -p <dbpassword>

2) Run db.products.createIndex( { title: "text", "description": "text" } )

3) db.products.getIndexes() should return something like:

{
    "v" : 2,
    "key" : {
        "_fts" : "text",
        "_ftsx" : 1
    },
    "name" : "title_text_description_text",
    "ns" : "database-name.products",
    "weights" : {
        "description" : 1,
        "title" : 1
    },
    "default_language" : "english",
    "language_override" : "language",
    "textIndexVersion" : 3
}

Note: products is your collection name

4) Try your query again!

MongoDB text-search reference