2016-08-24 73 views
0

我成功地在Node.js代碼中使用Mongoose運行$ text搜索。下面是我使用的代碼:

Model.find( 
    { $text : { $search : "FindThisString"}}, 
    { score : {$meta : "textScore"}} 
) 
.sort({ score : { $meta : 'textScore'}}) 
.exec(function(err, results) { 
    _.each(results, function(item) { 
     //console.log(item); 
     console.log(item._id); 
     console.log(item.score); 
    }); 
}); 

當我控制檯登錄整個文件,我可以看到控制檯上的「分數」領域,但「item.score」打印爲「不確定」。

如何在返回結果中訪問由MongoDB創建的分數?

+0

你能展示你的物品嗎?當你做'console.log(item)'時,它的外觀如何? –

+0

{_id:57bd960dd6499fef9dad4f01,cName:'XYZ',cAddress:'',__v:0,score:1.0073315117131936,contentSection:[],.....} –

+0

「分數」是結果中返回的字段。我可以訪問文檔的所有其他字段並處理/打印它們 - 除了分數。 –

回答

1

好吧,我理解了它必須做如下...什麼:

的console.log(item._doc.score);

這將做的伎倆!

+0

謝謝!無法找到關於此的很多信息。 –