$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 Consultants 公司內最近更新的存放區,請執行查詢以擷取 First Up Consultants 公司內的所有存放區、按 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 - 使用視窗運算子

若要擷取每個公司內最近更新的存放區,請執行查詢以按 company 欄位對結果進行分割,並按 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')"
  }
]