2016-08-24 71 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

谢谢!无法找到关于此的很多信息。 –