New in version 8.2.在版本8.2中新增。
Definition定义
$score$scorecomputes and returns a new score as metadata. It also optionally normalizes the input scores, by default to a range between zero and one.计算并返回新的分数作为元数据。它还可以选择将输入分数标准化,默认情况下为0到1之间的范围。
Syntax语法
The stage has the following syntax:该阶段具有以下语法:
{
$score: {
score: <expression>,
normalization: "none|sigmoid|minMaxScaler",
weight: <expression>
}
}Fields字段
$score takes the following fields:采用以下字段:
score | $meta keyword searchScore. $meta键searchScore中。$meta keyword searchScoreDetails. Returns an error for non-numeric inputs.$meta键searchScoreDetails中。对于非数字输入返回错误。 | |
normalization |
| |
weight | score expression by after normalization.score表达式乘以的数字。 |
Behavior行为
The output documents from a $score are the same as the input documents, but includes additional computed score as metadata.$score的输出文档与输入文档相同,但包含额外的计算得分作为元数据。
If you specify multiple 如果在管道中指定了多个$score stages in the pipeline, the last $score stage in the pipeline overrides the score metadata from previous $score stages.$score阶段,管道中的最后一个$score步骤将覆盖之前$score阶段的分数元数据。
Example示例
Consider the following documents in a collection named 考虑名为articles:articles的集合中的以下文档:
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 },
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
The following query uses the:以下查询使用:
$matchstage to filter the documents where the阶段用于筛选viewsfield value is greater than or equal to1000.views字段值大于或等于1000的文档。$scorestage to compute a new score that is normalized using计算一个新的分数,该分数使用$sigmoidexpression and then multiplied by the specified weight,1.$sigmoid表达式进行归一化,然后乘以指定的权重1。
db.articles.aggregate([
{
$match: {
views: { $gte: 1000 }
}
},
{
$score: {
score: "$score",
normalization: "sigmoid",
weight: 1
}
},
{
"$project": {
"_id": 0,
"author": 1,
"views": 1,
"score": 1,
"calculatedScore": { $meta: "score" }
}
}
])
[
{ author: 'ahn', score: 60, views: 1000, calculatedScore: 1 },
{ author: 'li', score: 55, views: 5000, calculatedScore: 1 },
{ author: 'ty', score: 95, views: 1000, calculatedScore: 1 }
]