On this page本页内容
Changed in version 5.0.在版本5.0中更改。
Returns the value that results from applying an expression to the last document in a group of documents. 返回将表达式应用于一组文档中的最后一个文档时产生的值。Only meaningful when documents are in a defined order.仅当文档处于定义的顺序时才有意义。
$last
is available in these stages:可在以下阶段使用:
$bucket
$bucketAuto
$group
$setWindowFields
This page describes the 此页面介绍$last
aggregation accumulator. $last
聚合累加器。For the 有关$last
array operator, see $last (array operator)
.$last
数组运算符,请参阅$last
(数组运算符)。
$last
syntax:语法:
{ $last: <expression> }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
To define the document order for 要使用定义$last
with the:$last
的文档顺序:
$group
stage, add a $sort
stage before the $group
stage.$group
阶段,在$group
阶段之前添加$sort
阶段。$setWindowFields
stage, set the sortBy field.$setWindowFields
阶段,设置sortBy
字段。Although the 尽管$sort
stage passes ordered documents as input to the $group
and $setWindowFields
stages, those stages are not guaranteed to maintain the sort order in their own output.$sort
阶段将有序文档作为输入传递给$group
和$setWindowFields
阶段,但这些阶段不能保证在自己的输出中维护排序顺序。
When used with 与$setWindowFields
, $last
returns null
for empty windows. $setWindowFields
一起使用时,对于空窗口,$last
返回null
。An example empty window is a 空窗口的示例是分区第一个文档上的{ documents: [ -1, -1 ] }
documents window on the first document of a partition.{ documents: [ -1, -1 ] }
文档窗口。
$group
Stage$group
阶段中使用Consider a 考虑使用以下文档进行sales
collection with the following documents:sales
集合:
{ "_id" : 1, "item" : "abc", "date" : ISODate("2014-01-01T08:00:00Z"), "price" : 10, "quantity" : 2 } { "_id" : 2, "item" : "jkl", "date" : ISODate("2014-02-03T09:00:00Z"), "price" : 20, "quantity" : 1 } { "_id" : 3, "item" : "xyz", "date" : ISODate("2014-02-03T09:05:00Z"), "price" : 5, "quantity" : 5 } { "_id" : 4, "item" : "abc", "date" : ISODate("2014-02-15T08:00:00Z"), "price" : 10, "quantity" : 10 } { "_id" : 5, "item" : "xyz", "date" : ISODate("2014-02-15T09:05:00Z"), "price" : 5, "quantity" : 10 } { "_id" : 6, "item" : "xyz", "date" : ISODate("2014-02-15T12:05:10Z"), "price" : 5, "quantity" : 5 } { "_id" : 7, "item" : "xyz", "date" : ISODate("2014-02-15T14:12:12Z"), "price" : 5, "quantity" : 10 }
The following operation first sorts the documents by 以下操作首先按item
and date
, and then in the following $group
stage, groups the now sorted documents by the item
field and uses the $last
accumulator to compute the last sales date for each item:item
和date
对文档进行排序,然后在下面的$group
阶段中,按项目字段对现在排序的文档进行分组,并使用$last
累加器计算每个项目的最后销售日期:
db.sales.aggregate( [ { $sort: { item: 1, date: 1 } }, { $group: { _id: "$item", lastSalesDate: { $last: "$date" } } } ] )
The operation returns the following results:该操作返回以下结果:
{ "_id" : "xyz", "lastSalesDate" : ISODate("2014-02-15T14:12:12Z") } { "_id" : "jkl", "lastSalesDate" : ISODate("2014-02-03T09:00:00Z") } { "_id" : "abc", "lastSalesDate" : ISODate("2014-02-15T08:00:00Z") }
$setWindowFields
Stage$setWindowFields
阶段中使用New in version 5.0.在版本5.0中新增。
Create a 创建包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
集合:
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 本例使用$last
in the $setWindowFields
stage to output the last cake sales order type
for each state
:$setWindowFields
阶段中的$last
输出每个州的最后一个蛋糕销售订单type
:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { lastOrderTypeForState: { $last: "$type", window: { documents: [ "current", "unbounded" ] } } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. state
对集合中的文档进行分区。CA
and WA
.CA
和WA
分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.orderDate
以升序(1
)对每个分区中的文档进行排序,因此最早的orderDate
居第一。output
sets the 将lastOrderTypeForState
field to the last order type
from the documents window.lastOrderTypeForState
字段设置为文档窗口中的最后一个订单type
。
The window contains documents between the 该窗口包含current
lower limit, which is the current document in the output, and the unbounded
upper limit. current
下限(即输出中的当前文档)和unbounded
上限之间的文档。This means 这意味着$last
returns the last order type
for the documents between the current document and the end of the partition.$last
返回当前文档和分区末尾之间文档的最后一个订单类型。
In this output, the last order 在此输出中,type
value for CA
and WA
is shown in the lastOrderTypeForState
field:CA
和WA
的最后一个订单type
值显示在lastOrderTypeForState
字段中:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "lastOrderTypeForState" : "vanilla" } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "lastOrderTypeForState" : "vanilla" } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "lastOrderTypeForState" : "vanilla" } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "lastOrderTypeForState" : "chocolate" } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "lastOrderTypeForState" : "chocolate" } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "lastOrderTypeForState" : "chocolate" }