【问题标题】:Can't use $text with String不能将 $text 与字符串一起使用
【发布时间】:2014-06-21 20:04:49
【问题描述】:

此代码转储到异常

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(){});

【问题讨论】:

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


    【解决方案1】:

    查看docs,您无法为文本搜索指定一个字段,它将搜索所有索引字段,因此在您的情况下,它将搜索 fullAddress 和 shortAddress 返回与这些字段中的任何一个匹配的文档。

    您的查询需要:

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

    现在应该会返回正确的数据。

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      相关资源
      最近更新 更多