此代码转储到异常
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
型号
shortAddress: {
type: String
},
索引
collection.ensureIndex({fullAddress: 'text', shortAddress: 'text'}, { default_language: "russian" },function(){});
最佳答案
看docs您不能为文本搜索指定一个字段,它将搜索所有索引字段,因此在您的情况下,它将搜索 fullAddress 和 shortAddress 返回与这些字段中的任何一个匹配的文档。
您的查询需要:
self.staticVars.Model
.find({$text : { $search: data.text }, _town: data._town},{limit: 10})
.populate('_street _district')
.sort({house: 1})
.exec(callback);
现在应该会返回正确的数据。
关于node.js - 不能将 $text 与字符串一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23473859/