Definition定义
Changed in version 5.0.在版本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使用指定的BSON比较顺序对不同类型的值进行值和类型比较。
$min is available in these stages:在以下阶段可用:
$addFields$bucket$bucketAuto$group包含$matchstage that includes an$exprexpression$expr表达式的$match阶段$project$replaceRoot$replaceWith$set$setWindowFields(Available starting in MongoDB 5.0)(从MongoDB 5.0开始可用)
Syntax语法
When used in the 在$bucket, $bucketAuto, $group, and $setWindowFields stages, $min has this syntax:$bucket、$bucketAuto、$group和$setWindowFields阶段中使用时,$min具有以下语法:
{ $min: <expression> }
When used in other supported stages, 当在其他支持的阶段中使用时,$min has one of two syntaxes:$min具有以下两种语法之一:
$minhas one specified expression as its operand:有一个指定的表达式作为操作数:{ $min: <expression> }$minhas a list of specified expressions as its operand:具有指定表达式的列表作为其操作数:{ $min: [ <expression1>, <expression2> ... ] }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
Result Type结果类型
$min compares input data following the BSON Comparison order to determine the appropriate output type. When the input data contains multiple data types, the $min output type is the lowest in the comparison order.$min按照BSON比较顺序比较输入数据,以确定适当的输出类型。当输入数据包含多种数据类型时,$min输出类型在比较顺序中最低。
Null or Missing Values值为空或缺失
If some, but not all, documents for the 如果$min operation have either a null value for the field or are missing the field, the $min operator only considers the non-null and the non-missing values for the field.$min操作的某些文档(但不是全部)的字段值为空或缺少字段,则$min运算符只考虑字段的非null值和非缺失值。
If all 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.$min操作的所有文档的字段值都为null或缺少字段,则$min运算符将返回null作为最小值。
Array Operand数组操作对象
In the 在$group and $setWindowFields stages, if the expression resolves to an array, $min does not traverse the array and compares the array as a whole.$group和$setWindowFields阶段,如果表达式解析为数组,$min不会遍历数组,而是将数组作为一个整体进行比较。
In the other supported stages:在其他支持的阶段:
With a single expression as its operand, if the expression resolves to an array,以单个表达式作为操作数,如果表达式解析为数组,则$mintraverses into the array to operate on the numeric elements of the array to return a single value.$min遍历数组以对数组的数字元素进行操作,从而返回单个值。With a list of expressions as its operand, if any of the expressions resolves to an array,使用表达式列表作为操作数,如果任何表达式解析为数组,$mindoes not traverse into the array but instead treats the array as a non-numeric value.$min不会遍历到数组中,而是将数组视为非数值。
Examples示例
Use in $group Stage在$group阶段中使用
$group StageConsider a 考虑一个包含以下文件的sales collection with the following documents:sales集合:
db.sales.insertMany( [
{ _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.item字段对文档进行分组,以下操作使用$min累加器计算每个分组的最小金额和最小数量。
db.sales.aggregate(
[
{
$group:
{
_id: "$item",
minQuantity: { $min: "$quantity" }
}
}
]
)
The operation returns the following results:该操作返回以下结果:
{ "_id" : "xyz", "minQuantity" : 5 }
{ "_id" : "jkl", "minQuantity" : 1 }
{ "_id" : "abc", "minQuantity" : 2 }Use in $project Stage在$project阶段中使用
$project StageA collection students contains the following documents:students集合包含以下文档:
db.students.insertMany( [
{ _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:$project阶段中的$min来计算最低测验分数、最低实验室分数以及期末和期中考试的最低分数:
db.students.aggregate([
{ $project: { quizMin: { $min: "$quizzes"}, labMin: { $min: "$labs" }, examMin: { $min: [ "$final", "$midterm" ] } } }
])
The operation results in the following documents:该操作产生了以下文件:
{ "_id" : 1, "quizMin" : 6, "labMin" : 5, "examMin" : 75 }
{ "_id" : 2, "quizMin" : 9, "labMin" : 8, "examMin" : 80 }
{ "_id" : 3, "quizMin" : 4, "labMin" : 5, "examMin" : 70 }Use in $setWindowFields Stage在$setWindowFields阶段中使用
$setWindowFields StageNew in version 5.0.在版本5.0中新增。
Create a 创建一个cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):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:$setWindowFields阶段使用$min来输出每个state的最小蛋糕销售quantity:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
minimumQuantityForState: {
$min: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"partitions the documents in the collection by按state. There are partitions forCAandWA.state对集合中的文档进行分区。CA和WA有分区。sortBy: { orderDate: 1 }sorts the documents in each partition by按orderDatein ascending order (1), so the earliestorderDateis first.orderDate升序(1)对每个分区中的文档进行排序,因此最早的orderDate位居前列。
outputsets theminimumQuantityForStatefield to the minimumquantityvalue using$minthat is run in a documents window.output使用在documents窗口中运行的$min将minimumQuantityForState字段设置为最小quantity值。The window contains documents between an该窗口包含的文档位于unboundedlower limit and thecurrentdocument in the output. This means$minreturns the minimumquantityfor the documents between the beginning of the partition and the current document.unbounded下限和输出中的current文档之间。这意味着$min返回分区开始和当前文档之间的文档的最小quantity。
In this output, the minimum 在此输出中,quantity for CA and WA is shown in the minimumQuantityForState field:CA和WA的最小quantity显示在minimumQuantityForState字段中:
{ "_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 }