node.js - Mongoose $text 字段错误

标签 node.js mongodb mongoose

我试图在 Mongoose 中使用`$text,但它总是向我显示错误。

模型/架构:

var thSchema2=new Schema({
    userid:{ type: String, unique: true,index: true},
    password:String,
    name:String,
    phone:String
});

我在 Controller 中的查询:

Model
    .find( { $text : { $search : "mo"} })
    .exec(function(err, results) {
        if(err){
            console.log("eroro ocured");
            res.send(401);
        }else{
            console.log("foud");
            res.json(results);
        }
    });

它总是向我显示错误。

最佳答案

您的架构中缺少text索引,如果您想要搜索字段name,请添加如下文本索引

thSchema2.index({'name': 'text'});

关于node.js - Mongoose $text 字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005381/

相关文章:

node.js - 如何像 Google 文档一样在羽毛笔编辑器中执行跟踪更改?

mongodb - 如何在结构未知的集合中进行搜索?

node.js - Mongoose 更新嵌入文档

node.js - 在 Nodejs 中执行 npm test 进行 Mocha 测试时,“make”未被识别为内部或外部命令

mysql - 无法从已部署的 Kubernetes NodeJS 应用程序连接到 Google MySQL

node.js - 解析服务器 - 如何使用调度程序一遍又一遍地运行 'jobs'

ruby-on-rails - Rails 和 Mongoid::将字符串转换为数组

node.js - mongoDB中的update和updateMany方法有什么区别?

node.js - 使用mongodb无法通过id获取数据

node.js - Mongoose 嵌套填充查询