Database Manual / Reference / Query Language / Expressions

$minMaxScaler (Window Function)(窗口函数)

Definition定义

New in version 8.2.在版本8.2中新增。

$minMaxScaler

Normalizes a numeric expression within a window of values. By default, values can range between zero and one. The smallest value becomes zero, the largest value becomes one, and all other values scale proportionally in between zero and one. You can also specify a custom minimum and maximum value for the normalized output range.在值窗口内规范化数值表达式。默认情况下,值的范围可以在零到一之间。最小值变为零,最大值变为一,所有其他值在零和一之间按比例缩放。您还可以为标准化输出范围指定自定义的最小值和最大值。

$minMaxScaler is only available in the $setWindowFields stage.仅在$setWindowFields阶段可用。

$minMaxScaler window operator has the following syntax:窗口运算符具有以下语法:

{ $minMaxScaler: <numeric expression> }

The value can be:该值可以是:

  • A numeric expression, which is the value that you want to normalize. It can be a specific numeric field or value calculated from your documents.一个数值表达式,它是您要规范化的值。它可以是一个特定的数字字段,也可以是根据文档计算的值。
  • A document in the following format:以下格式的文件:

    {
    input: <numeric expression>,
    min: <constant numeric expression>,
    max: <constant numeric expression>
    }
    Field字段Description描述
    inputNumeric expression, which contains the value that you want to normalize.数值表达式,其中包含要规范化的值。
    minMinimum value that you want in the output. If omitted, defaults to 0.输出中所需的最小值。如果省略,则默认为0
    maxMaximum value that you want in the output. If omitted, defaults to 1.您希望输出的最大值。如果省略,则默认为1

Behavior行为

$minMaxScaler uses the following formula to normalize the numeric expression:使用以下公式对数值表达式进行规范化:

minMaxScaler(x, min, max) = ((x - min(X)) / (max(X) - min(X))) * (max - min) + min

Where:

xValue to normalize.要标准化的值。
minDesired minimum value of outputs.输出的期望最小值。
maxDesired maximum value of outputs.期望的最大输出值。
min(X)Minimum value in the range.范围内的最小值。
max(X)Maximum value in the range.范围内的最大值。

The $minMaxScaler returns an error if the input value is any of the following:如果input值为以下任何值,$minMaxScaler将返回错误:

  • Non-numeric
  • Null
  • Empty arrays
  • Strings

Examples示例

Suppose your documents in your collection have a field named a with the following values:假设集合中的文档有一个名为a的字段,其值如下:

{ "_id": 1, "a": 1 }
{ "_id": 2, "a": 5 }
{ "_id": 3, "a": 13 }
{ "_id": 4, "a": 21 }

Consider the following pipeline stage and the output:考虑以下管道阶段和输出:

db.example.aggregate([
{$setWindowFields: {
sortBy: {a: 1},
output: {
scaled: {$minMaxScaler: "$a"},
scaledTo100: {$minMaxScaler: {input: "$a", min: 0, max: 100}},
}
}}
])
{a: 1, scaled: 0, scaledTo100: 0}
{a: 5, scaled: 0.2, scaledTo100: 20}
{a: 13, scaled: 0.6, scaledTo100: 60}
{a: 21, scaled: 1, scaledTo100: 100}

In the preceding example, the pipeline uses the $minMaxScaler to calculate two scaled values:在前面的示例中,管道使用$minMaxScaler计算两个缩放值:

  • scaled, which applies the default values, 0 and 1, to scale.scaled,它将默认值01应用于缩放。
  • scaledTo100, which applies a range between 0 and 100 to scale.,它将0100之间的范围应用于缩放。

The output shows the original value of a and the two scaled values. 输出显示了a的原始值和两个缩放值。The $minMaxScaler uses the following for the documents, where min(X) is 1 and max(X) is 21 (calculated from the documents), to return the scaled values:$minMaxScaler对文档使用以下内容,其中min(X)1max(X)21(根据文档计算),以返回缩放值:

{a: 1}
scaled = ((1 - 1) / (21 - 1)) * (1 - 0) + 0 = 0
scaledTo100 = ((1 - 1) / (21 - 1)) * (100 - 0) + 0 = 0
{a: 5}
scaled = ((5 - 1) / (21 - 1)) * (1 - 0) + 0 = (4 / 20) * 1 + 0 = 0.2
scaledTo100 = ((5 - 1) / (21 - 1)) * (100 - 0) + 0 = (4 / 20) * 100 + 0 = 20
{a: 13}
scaled = ((13 - 1) / (21 - 1)) * (1 - 0) + 0 = (12 / 20) * 1 + 0 = 0.6
scaledTo100 = ((13 - 1) / (21 - 1)) * (100 - 0) + 0 = (12 / 20) * 100 + 0 = 60
{a: 21}
scaled = ((21 - 1) / (21 - 1)) * (1 - 0) + 0 = (20 / 20) * 1 + 0 = 1
scaledTo100 = ((21 - 1) / (21 - 1)) * (100 - 0) + 0 = (20 / 20) * 100 + 0 = 100