Docs Menu
Docs Home
/ /

$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>,
scoreDetails: <boolean>,
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 score. Returns an error for non-numeric inputs.

scoreDetails

Boolean

Default is false. Specifies if $score computes and populates the $scoreDetails metadata field for each output document. See scoreDetails for more information on this field.

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.

If you set scoreDetails to true, $score creates a scoreDetails metadata field for each document. The scoreDetails field contains information about the final ranking.

Note

When you set scoreDetails to true, $score sets the scoreDetails metadata field for each document but does not automatically output the scoreDetails metafield.

To view the scoreDetails metadata field, you must either:

  • use a $project stage after $score to project the scoreDetails field

  • use a $addFields stage after $score to add the scoreDetails field to your pipeline output

The scoreDetails field contains the following subfields:

Field
Description

value

The calculated score.

description

A description in string format that explains how the $score operationMetrics calculates the final score.

rawScore

The unweighted raw (not normalized) score.

normalization

One of the following normalization values: "none", "sigmoid", "minMaxScaler".

weight

The weight of the input pipeline, which must be in range [0,1].

expression

The value of $score.score in string format.

details

An empty array, as $score cannot take any input pipelines.

Warning

MongoDB does not guarantee any specific output format for scoreDetails.

For example, the following code blocks shows the scoreDetails field for a $score operation with an $add expression:

{
_id: ObjectId('55f5a192d4bede9ac365b257'),
scoreDetails: {
value: 0.5,
description: 'the score calculated from multiplying a weight in the range [0,1] with either a normalized or nonnormalized value:',
rawScore: 145,
normalization: 'sigmoid',
weight: 0.5,
expression: '{ string: { $add: [ 'field', 'myField' ] } }',
details: []
}
},

Back

$sample

On this page