【发布时间】:2016-12-31 21:34:34
【问题描述】:
我成功地在 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);
});
});
当我在控制台记录整个文档时,我可以在控制台上看到“score”字段,但“item.score”打印为“undefined”。
如何在返回的结果中访问 MongoDB 创建的分数?
【问题讨论】:
-
您可以展示您的商品吗?当你做
console.log(item)时的样子 -
{ _id: 57bd960dd6499fef9dad4f01, cName: 'XYZ', cAddress: ' ', __v: 0, score: 1.0073315117131936, contentSection: [], .....}
-
“score”是结果中返回的字段。我可以访问文档的所有其他字段并处理/打印它们 - 除了分数。
标签: node.js mongodb mongoose mongodb-query text-search