> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $score (aggregation)

**New in version 8.2**

## Definition

`$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.

## Syntax

The stage has the following syntax:

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

## Fields

`$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`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta.md#mongodb-expression-exp.-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](https://www.mongodb.com/docs/manual/reference/operator/aggregation/score.md#std-label-score-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`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sigmoid.md#mongodb-expression-exp.-sigmoid) expression:`1 / (1 + e^-x)`Here, `x` is the score to normalize.If omitted, defaults to `none`.; `minMaxScaler` - to apply the [`$minMaxScaler`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/minmaxscaler.md#mongodb-group-grp.-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. |

## Behavior

The output documents from a [`$score`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/score.md#mongodb-pipeline-pipe.-score) are the same as the input documents, but includes additional computed score as metadata.

If you specify multiple [`$score`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/score.md#mongodb-pipeline-pipe.-score) stages in the pipeline, the last [`$score`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/score.md#mongodb-pipeline-pipe.-score) stage in the pipeline overrides the score metadata from previous [`$score`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/score.md#mongodb-pipeline-pipe.-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 [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project) stage after `$score` to project the `scoreDetails` field

- use a [`$addFields`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/addfields.md#mongodb-pipeline-pipe.-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:

```js&#xA;
 {
  _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: []
  }
},
```
