Docs Menu
Docs Home
/
Database Manual
/ / /

$score (aggregation)

New in version 8.2.

$score

$score computes and returns a new score as metadata. It also optionally normalizes the input scores, by default to a range between zero and one.

The stage has the following syntax:

{
$score: {
score: <expression>,
normalization: "none|sigmoid|minMaxScaler",
weight: <expression>
}
}

$score takes the following fields:

Field
Type
Description

score

Expression

Computes a new value from the input scores and stores the value in the $meta keyword searchScore. All other scores are stored in the $meta keyword searchScoreDetails. Returns an error for non-numeric inputs.

normalization

String

Optional. Normalizes the score to the range of 0 to 1. Value can be:

  • none - Doesn't normalize.

  • sigmoid - Applies the following $sigmoid expression:

    1 / (1 + e^-x)

    Here, x is the score to normalize.

    If omitted, defaults to none.

  • minMaxScaler - to apply the $minMaxScaler window function:

    (s - min_s) / (max_s - min_s)

    Where:

    • s is the score.

    • min_s is the minimum score.

    • max_s is the maximum score.

weight

Double

Optional. Number to multiply the score expression by after normalization.

The output documents from a $score are the same as the input documents, but includes additional computed score as metadata.

If you specify multiple $score stages in the pipeline, the last $score stage in the pipeline overrides the score metadata from previous $score stages.

Consider the following documents in a collection named 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:

  • $match stage to filter the documents where the views field value is greater than or equal to 1000.

  • $score stage to compute a new score that is normalized using $sigmoid expression and then multiplied by the specified weight, 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 }
]

Back

$sample

On this page