Docs 菜单

$min (aggregation)

5.0 版本中的更改

$min

Returns the minimum value. $min compares both value and type, using the specified BSON comparison order for values of different types.

$min 可在以下阶段使用:

当在 $bucket$bucketAuto$group$setWindowFields 阶段使用时,$min 具有以下语法:

{ $min: <expression> }

在其他受支持的阶段中使用时,$min 具有两种语法之一:

  • $min将一个指定表达式作为其操作数:

    { $min: <expression> }
  • $min将指定表达式的列表作为其操作数:

    { $min: [ <expression1>, <expression2> ... ] }

有关表达式的更多信息,请参阅表达式运算符。

如果 $min 操作的部分(但不是全部)文档的该字段值为 null 或缺少该字段,则 $min 操作符仅考虑该字段为非空值和非缺失值的情况。

If 所有 documents for the $min operation have null value for the field or are missing the field, the $min operator returns null for the minimum value.

$group$setWindowFields 阶段,如果表达式解析为数组,$min 不会遍历该数组并将其作为一个整体进行比较。

在其他支持的阶段:

  • 使用单个表达式作为其操作数,如果表达式解析为大量,则$min 会遍历该大量,对大量的数字元素进行操作以返回单个值。

  • 将表达式列表作为其操作数,如果任何表达式解析为大量,$min 不会遍历该大量,而是将该大量视为非数字值。

请考虑包含以下文档的 sales 集合:

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }

Grouping the documents by the item field, the following operation uses the $min accumulator to compute the minimum amount and minimum quantity for each grouping.

db.sales.aggregate(
[
{
$group:
{
_id: "$item",
minQuantity: { $min: "$quantity" }
}
}
]
)

操作返回以下结果:

{ "_id" : "xyz", "minQuantity" : 5 }
{ "_id" : "jkl", "minQuantity" : 1 }
{ "_id" : "abc", "minQuantity" : 2 }

集合 students 包含以下文档:

{ "_id": 1, "quizzes": [ 10, 6, 7 ], "labs": [ 5, 8 ], "final": 80, "midterm": 75 }
{ "_id": 2, "quizzes": [ 9, 10 ], "labs": [ 8, 8 ], "final": 95, "midterm": 80 }
{ "_id": 3, "quizzes": [ 4, 5, 5 ], "labs": [ 6, 5 ], "final": 78, "midterm": 70 }

The following example uses the $min in the $project stage to calculate the minimum quiz scores, the minimum lab scores, and the minimum of the final and the midterm:

db.students.aggregate([
{ $project: { quizMin: { $min: "$quizzes"}, labMin: { $min: "$labs" }, examMin: { $min: [ "$final", "$midterm" ] } } }
])

该操作生成以下文档:

{ "_id" : 1, "quizMin" : 6, "labMin" : 5, "examMin" : 75 }
{ "_id" : 2, "quizMin" : 9, "labMin" : 8, "examMin" : 80 }
{ "_id" : 3, "quizMin" : 4, "labMin" : 5, "examMin" : 70 }

版本 5.0 中的新增功能

创建cakeSales集合,其中包含加利福尼亚州 ( CA ) 和华盛顿州 ( WA ) 的蛋糕销售情况:

db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )

This example uses $min in the $setWindowFields stage to output the minimum quantity of cake sales for each state:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
minimumQuantityForState: {
$min: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

在示例中:

  • partitionBy: "$state"state 对集合中的文档分区CAWA 都有分区。

  • sortBy: { orderDate: 1 }orderDate 升序 (1) 对每个分区中的文档进行排序,因此最早的 orderDate 位于最前面。

  • output sets the minimumQuantityForState field to the minimum quantity value using $min that is run in a 文档 window.

    The 窗口 contains documents between an unbounded lower limit and the current document in the output. This means $min returns the minimum quantity for the documents between the beginning of the partition and the current document.

In this output, the minimum quantity for CA and WA is shown in the minimumQuantityForState field:

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "minimumQuantityForState" : 162 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "minimumQuantityForState" : 120 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "minimumQuantityForState" : 120 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "minimumQuantityForState" : 134 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "minimumQuantityForState" : 104 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "minimumQuantityForState" : 104 }