2016-09-19 65 views
0

我有5個記錄,當我試圖讓那些我對着下面的錯誤,有人可以幫我please.Thanks在轉換圓形結構,以JSON節點JS

我的NodeJS,

exports.searchlistbyfirstname = function (req, res) { 
    var params = req.params; 
    var record= db.collection('profile'); 
    record.find({ $text: { $search: params.id} }, (err, result) => { 
    if (err){ return console.log(err) 
    } 
     if(result){ 
      response = {status:'success',data:result}; 
     } else{ 
      response = {status:'fail'}; 
     } 
     res.send(response); 
    }); 

}; 
+0

如果使用了'params'變量?嘗試使用遊標的'toArray()'方法獲取數據作爲'record.find()。toArray((err,result)=> {...});' – chridam

+0

Cridam,我沒有在這裏使用任何參數,我只想得到我的db中的總記錄數 – MMR

+0

謝謝,我明白了...... – MMR

回答

0

當你想搜索一個集合中的多個字段時,$ text和$ search是很有用的,就像你想要搜索一個字符串一樣,可以在多個字段中,比如firstname,lastname等,這樣你就可以按如下方式創建索引: 模式等級:

new Schema({ 
    firstname:{type: String, index: true}, 
    firstname:{type: String, index: true} 
    ..... 
}); 

使用ensureIndex:

profile.collection.ensureIndex({firstname:"text",lastname:"text" ....},default_language:"none"},function(err,result){ }); 

使用$文本和$搜索,你可以在其他語言的搜索,以及像:

你有兩個記錄:

{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 } 
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 } 

並且您想用西班牙語搜索,那麼您可以按以下方式進行搜索:

db.articles.find(
    { $text: { $search: "leche", $language: "es" } } 
) 

查詢返回下列文件:

{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 } 
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 } 

欲瞭解更多詳情,請參閱此鏈接: https://docs.mongodb.com/manual/reference/operator/query/text/

相關問題