2014-05-05 55 views
2

此代碼$文本轉儲到異常不能使用字符串

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

異常

Can't use $text with String 

型號

shortAddress: { 
    type: String 
}, 

指數

collection.ensureIndex({fullAddress: 'text', shortAddress: 'text'}, { default_language: "russian" },function(){}); 

回答

3

查看docs您不能爲文本搜索指定一個字段,它將搜索所有索引字段,因此在您的情況下,它將搜索fullAddress和shortAddress,以返回與這兩個字段中的任何一個匹配的文檔。

您的查詢將需要:

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

這個現在應該回到你正確的數據。