Pooja
Pooja

Reputation: 1

Not able to create index using mongoose

I am building a search feature in a Node.js application which uses Mongoose, creating a full-text search index in Mongo:

postSchema = mongoose.Schema({
        xx : String
        .......
    }).index({
          'xx':'text'
        });
    Post = mongoose.model('Post',postSchema);

And execute the search as follows:

find = {'$text':{'$search':query}};
findScore = {'score':{'$meta':'textScore'}};
sort = {'score': {'$meta':'textScore'} };
Post.find(find, findScore).sort(sort).exec(function (err, data) {
          if (err){
            logging.log(err);
          } else {
            logging.log(data);
          }
                });

When I run the program, I get the following error: 'Unable to execute query: error processing query \n planner returned error: need exactly one text index for $text query'

Any help would be appreciated.

Upvotes: 0

Views: 808

Answers (1)

Atish
Atish

Reputation: 4417

A collection can have at most one text index. You need to check it any of the text index is previously created.

Upvotes: 1

Related Questions