2016-03-15 58 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