如何访问 Text Score MongoDB $text search

Ash*_*shi 2 text-search mongoose mongodb node.js mongodb-query

我成功地在 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);
     });
});
Run Code Online (Sandbox Code Playgroud)

当我控制台记录整个文档时,我可以在控制台上看到“score”字段,但“item.score”打印为“undefined”。

如何在返回的结果中访问 MongoDB 创建的分数?

Ash*_*shi 5

好吧,我想通了……需要做的事情如下:

console.log(item._doc.score);

这样就行了!