$last

运算符 $last 对查询指定的一个或多个字段上的文档进行排序,并返回与筛选条件匹配的最后一个文档。

语法

{
  "$last": <expression>
}

参数

参数 DESCRIPTION
expression 要计算和返回结果集中最后一个文档的表达式

例子

请考虑存储集合中的此示例文档。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

示例 1:获取公司内上次更新的商店

若要检索 First Up 顾问公司内最近更新的存储,请运行查询以提取 First Up 顾问中的所有存储,按 lastUpdated 字段的升序对文档进行排序,并从已排序的结果中返回最后一个文档。

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        }
    }
}, {
    $sort: {
        lastUpdated: 1
    }
}, {
    $group: {
        _id: "$company",
        lastUpdated: {
            $last: "$lastUpdated"
        }
    }
}])

此查询返回以下结果:

[
  {
      "_id": "First Up Consultants",
      "lastUpdated": "ISODate('2024-12-31T13:01:19.097Z')"
  }
]

示例 2 - 使用窗口运算符

若要检索每个公司中最近更新的存储,请运行查询,按公司字段对结果进行分区,并按 lastUpdated 字段的升序对每个分区中的文档进行排序,并返回每个分区的已排序结果。

db.stores.aggregate([{
    $setWindowFields: {
        partitionBy: "$company",
        sortBy: {
            lastUpdated: 1
        },
        output: {
            lastUpdatedDateForStore: {
                $last: "$lastUpdated",
                window: {
                    documents: ["current", "unbounded"]
                }
            }
        }
    }
}])

此查询返回的第一个结果为:

[
  {
      "_id": "First Up Consultants",
      "lastUpdated": "ISODate('2024-12-31T13:01:19.097Z')"
  }
]