New in version 8.2.
Definition
Syntax
The stage has the following syntax:
{ $score: { score: <expression>, scoreDetails: <boolean>, normalization: "none|sigmoid|minMaxScaler", weight: <expression> } }
Fields
$score takes the following fields:
Field | Type | Description | ||
|---|---|---|---|---|
| Expression | Computes a new value from the input scores and stores the value
in the | ||
| Boolean | Default is | ||
| String | Optional. Normalizes the score to the range of
| ||
| Double | Optional. Number to multiply the |
Behavior
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.
scoreDetails
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
$projectstage after$scoreto project thescoreDetailsfielduse a
$addFieldsstage after$scoreto add thescoreDetailsfield to your pipeline output
The scoreDetails field contains the following subfields:
Field | Description |
|---|---|
| The calculated score. |
| A description in string format that explains how the |
| The unweighted raw (not normalized) score. |
| One of the following normalization values: "none", "sigmoid", "minMaxScaler". |
| The weight of the input pipeline, which must be in range [0,1]. |
| The value of |
| An empty array, as |
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: [] } },