On this page本页内容
$last
New in version 4.4.在版本4.4中新增。
Returns the last element in an array.返回数组中最后一个元素。
This page describes the 此页面介绍$last array operator. $last数组运算符。For the 对于$last aggregation accumulator, see $last (aggregation accumulator).$last聚合累加器,请参阅$last(聚合累加符)。
The $last operator has the following syntax:$last运算符语法如下:
{ $last: <expression> }
The <expression> can be any valid expression as long as it resolves to an array, null, or missing. <expression>可以是任何有效的表达式,只要它解析为数组、null或缺失。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
The $last operator is an alias for the following $arrayElemAt expression:$last运算符是以下$arrayElemAt表达式的别名:
{ $arrayElemAt: [ <array expression>, -1 ] }
Valid operand for $last must resolve to an array, null, or missing.$last的有效操作数必须解析为数组、null或缺失。
$last returns the first element in the array:$last将返回数组中的第一个元素:[], $last does not return a value.[],则$last不返回值。$last returns null.null或缺失,$last将返回null。For example, create a test collection 例如,使用以下文档创建测试集合example1 with the following documents:example1:
db.example1.insertMany([
{ "_id" : 1, "x" : [ 1, 2, 3 ] },
// Non-empty array
{ "_id" : 2, "x" : [ [ ] ] },
// Non-empty array
{ "_id" : 3, "x" : [ null ] },
// Non-empty array
{ "_id" : 4, "x" : [ ] },
// Empty array
{ "_id" : 5, "x" : null },
// Is null
{ "_id" : 6 }
// Is Missing
])
Then, the following adds a new field 然后,下面添加一个新字段lastElem whose value is derived from applying the $last operator to the x field:lastElem,该字段的值是通过将$last运算符应用于x字段而得到的:
db.example1.aggregate([
{ $addFields: { lastElem: { $last: "$x" } } }
])
The operator returns the following documents:运算符返回以下文档:
{ "_id" : 1, "x" : [ 1, 2, 3 ], "lastElem" : 3 }
{ "_id" : 2, "x" : [ [ ] ], "lastElem" : [ ] }
{ "_id" : 3, "x" : [ null ], "lastElem" : null }
{ "_id" : 4, "x" : [ ] }
// No output
{ "_id" : 5, "x" : null, "lastElem" : null }
{ "_id" : 6, "lastElem" : null }
If the operand does not resolve to an array, null, or missing, the aggregation operation as a whole errors.如果操作数未解析为数组、null或缺失,则聚合操作将整体出错。
For example, create a test collection 例如,使用以下文档创建测试集合example2 with the following documents:example2:
db.example2.insertMany([
{ "_id" : 1, "x" : [ 1, 2, 3 ] },
{ "_id" : 2, "x" : 2 },
// x is not an array/null or missing
])
Then, the following aggregation operation returns an error because of the 然后,由于{ "_id" : 2, "x" : 2 } document:{ "_id" : 2, "x" : 2 }文档,以下聚合操作返回错误:
db.example2.aggregate( { $addFields: { lastElem: { $last: "$x" } } } )
That is, the operation returns the following:也就是说,该操作返回以下结果:
2020-01-20T21:12:26.713-05:00 E QUERY [js] uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "$last's argument must be an array, but is double", "code" : 28689, "codeName" : "Location28689" } : aggregate failed :
Create a sample collection 使用以下文档创建示例集合runninglog with the following documents:runninglog:
db.runninglog.insertMany([
{ "_id" : 1, "team" : "Anteater", log: [ { run: 1, distance: 8 }, { run2: 2, distance: 7.5 }, { run: 3, distance: 9.2 } ] },
{ "_id" : 2, "team" : "Bears", log: [ { run: 1, distance: 18 }, { run2: 2, distance: 17 }, { run: 3, distance: 16 } ] },
{ "_id" : 3, "team" : "Cobras", log: [ { run: 1, distance: 2 } ] }
])
The following aggregation uses the 以下聚合使用$first and $last operator on the log array to retrieve the information for the first run and the last run:log数组上的$first和$last运算符检索第一次运行和最后一次运行的信息:
db.runninglog.aggregate([
{ $addFields: { firstrun: { $first: "$log" }, lastrun: { $last: "$log" } } }
])
The operation returns the following results:该操作返回以下结果:
{ "_id" : 1, "team" : "Anteater", "log" : [ { "run" : 1, "distance" : 8 }, { "run2" : 2, "distance" : 7.5 }, { "run" : 3, "distance" : 9.2 } ],
"firstrun" : { "run" : 1, "distance" : 8 }, "lastrun" : { "run" : 3, "distance" : 9.2 } }
{ "_id" : 2, "team" : "Bears", "log" : [ { "run" : 1, "distance" : 18 }, { "run2" : 2, "distance" : 17 }, { "run" : 3, "distance" : 16 } ],
"firstrun" : { "run" : 1, "distance" : 18 }, "lastrun" : { "run" : 3, "distance" : 16 } }
{ "_id" : 3, "team" : "Cobras", "log" : [ { "run" : 1, "distance" : 2 } ],
"firstrun" : { "run" : 1, "distance" : 2 }, "lastrun" : { "run" : 1, "distance" : 2 } }
To calculate the change between the first and the last distances, the following operation uses 要计算第一个距离和最后一个距离之间的变化,如果$cond and $size operators to calculate the difference (i.e. $subtract) the two distances if there are two or more elements in the log array:log数组中有两个或更多元素,以下操作将使用$cond和$size运算符计算两个距离的差值(即$subtract):
db.runninglog.aggregate([
{ $addFields: { firstrun: { $first: "$log" }, lastrun: { $last: "$log" } } },
{ $project: { team: 1, progress:
{
$cond: {
if: { $gt: [ { $size:"$log" }, 1 ] } ,
then: { $subtract: [ "$lastrun.distance", "$firstrun.distance"] },
else: "Not enough data." }
}
}}
])
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 1, "team" : "Anteater", "progress" : 1.1999999999999993 }
{ "_id" : 2, "team" : "Bears", "progress" : -2 }
{ "_id" : 3, "team" : "Cobras", "progress" : "Not enough data." }
By default, 默认情况下,mongosh uses the 64-bit floating-point double for numbers. mongosh对数字使用64位双精度浮点。To improve precision, you can use NumberDecimal instead.要提高精度,可以改用NumberDecimal。