Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use $text with String

This code dumped to exception

self.staticVars.Model
        .find({shortAddress: {$text : { $search: data.text }}, _town: data._town},{limit: 10})
        .populate('_street _district')
        .sort({house: 1})
        .exec(callback);

Exception

Can't use $text with String

Model

shortAddress: {
    type: String
},

Index

collection.ensureIndex({fullAddress: 'text', shortAddress: 'text'}, { default_language: "russian" },function(){});
like image 879
Mike Bazhenov Avatar asked May 05 '14 13:05

Mike Bazhenov


1 Answers

Looking at the docs you cannot specify a field for the text search, it will search across all the indexed fields, so in your case it will search over fullAddress and shortAddress returning documents that match in either of these fields.

Your query would need to be:

self.staticVars.Model
    .find({$text : { $search: data.text }, _town: data._town},{limit: 10})
    .populate('_street _district')
    .sort({house: 1})
    .exec(callback);

This should now return you the correct data.

like image 119
Alistair Nelson Avatar answered Oct 23 '22 16:10

Alistair Nelson