Definition定义
$last
Changed in version 5.0.在版本5.0中的更改。
Returns the result of an expression for 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(Available starting in MongoDB 5.0)(从MongoDB 5.0开始可用)
Syntax语法
$last syntax:
{ $last: <expression> }Behaviors行为
Defining Document Order定义文档顺序
Array Operator数组运算符
If the expression resolves to an array:如果表达式解析为数组:
For a group of documents, as with the对于一组文档,与$groupand$setWindowFieldsstages,$lastreturns the entire array from the last document. It does not traverse array elements.$group和$setWindowFields阶段一样,$last返回最后一个文档的整个数组。它不遍历数组元素。For an individual document, as with the对于单个文档,与$addFieldsstage,$lastreturns the last element of the array.$addFields阶段一样,$last返回数组的最后一个元素。
Missing Values缺失值
Documents in a group may be missing fields or may have fields with missing values.组中的文档可能缺少字段,或者可能有缺少值的字段。
If there are no documents from the prior pipeline stage, the如果没有来自前一个管道阶段的文档,则$groupstage returns nothing.$group阶段将不返回任何内容。If the field that the如果$lastaccumulator is processing is missing,$lastreturnsnull.$last累加器正在处理的字段丢失,$last将返回null。When used with当与$setWindowFields,$lastreturnsnullfor empty windows. For example, when you have a{ documents: [ -1, -1] }documentswindow on the first document of a partition.$setWindowFields一起使用时,$last对于空窗口返回null。例如,当您在分区的第一个文档上有一个{ documents: [ -1, -1] }documents窗口时。
For more information, see the Missing Data example later in this topic.有关更多信息,请参阅本主题后面的缺少数据示例。
Examples示例
Use in $group Stage在$group Stage中使用
$group StageConsider a 考虑一个包含以下文件的sales collection with the following documents:sales集合:
db.sales.insertMany( [
{ _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阶段,按item字段对现在排序的文档进行分组,并使用$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") }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 此示例在$last in the $setWindowFields stage to output the last cake sales order type for each state:$setWindowFields阶段使用$last输出每个state的最后一个蛋糕销售订单type:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
lastOrderTypeForState: {
$last: "$type",
window: {
documents: [ "current", "unbounded" ]
}
}
}
}
}
] )
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 thelastOrderTypeForStatefield to the last ordertypefrom thedocumentswindow.output将lastOrderTypeForState字段设置为documents窗口中的最后一个订单type。The该windowcontains documents between thecurrentlower limit, which is the current document in the output, and theunboundedupper limit.window包含current下限(即输出中的当前文档)和unbounded上限之间的文档。This means这意味着$lastreturns the last ordertypefor the documents between the current document and the end of the partition.$last返回当前文档和分区末尾之间的文档的最后一个订单type。
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" }