node.js - 不能将 $text 与字符串一起使用

标签 node.js mongodb mongoose node-mongodb-native

此代码转储到异常

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/

相关文章:

regex - MongoDB:哪个更快?正则表达式搜索字符串还是直接搜索数组?

node.js - 资源状态 500。消息 : Internal server error find product by name and price

node.js - 使用 mongoose Schema.methods 函数运行应用程序时出错

javascript - Passport-Facebook 和 JavaScript Facebook SDK

javascript - 使用javascript对数组进行排序由字母和数字组成

javascript - 错误 : Callback was already called when using pg-promise with async series

node.js - 使用 nodeJS 和 mongoose 将外部 JSON 文件导入到 MongoDB

javascript - 仅在 mongoose .find() 上返回文档 _id

node.js - 在数组元素中添加子文档

linux - 在位置 2 安装带有 NVM 不匹配支架的 NPM