Wednesday 3 May 2017

Mongoose, text search multiple fields not working

Here is my model,

 var InternetSchema = new Schema({
  name: String,
  company: String,
  contactNumber:String,
  accessToken:String,
});

InternetSchema.index({name: 'text', company: 'text');
export default mongoose.model('Internet', InternetSchema);

And here is my function that responds to the search API

export function getSearchAccess(req, res) {
    // const arr = [
    //     {name: req.params.term},
    //     {company: req.params.term}
    // ]
    console.log(req.params.term)
    Internet.find({
       $text: {
           $search: req.params.term
       }
    }).limit(10).exec(function(finderr, finddata) {
        return res.json({ count: 10, data: finddata });
    });

}

But, this only seems to fetch documents that match the name field. It doesn't match the company field.

I have tried testing it in mongo shell, and it doesn't fetch any data for the company but does not fetch data for the name



via Ayush Bahuguna

No comments:

Post a Comment