2016-03-15 60 views
1

我想在猫鼬中使用`$文本,但它总是向我显示错误。

模型/方案:

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

我在控制器中查询:

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

它总是显示我的错误。

回答

2

你缺少text索引架构中的,如果你想在搜索领域name,然后添加文字指数如下

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

我想搜索的用户ID,我已经指定索引:在架构 –

+0

真@MohammedGadiwala,尝试使用'thSchema2.index({'userid':'text'});','index:true'与文本索引不同。 – zangw

+0

@MohammedGadiwala,这里有一个关于猫鼬的文本索引的讨论,https://github.com/Automattic/mongoose/issues/1401 – zangw