$first

$first運算子會在查詢指定的一或多個字段上排序檔,並傳回符合篩選準則的第一份檔。 如果未指定排序次序,則順序為未定義。

語法

{
    $first: <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",
        firstUpdated: {
            $first: "$lastUpdated"
        }
    }
}])

此查詢會傳回下列結果:

[
  {
      "_id": "First Up Consultants",
      "firstUpdated": "ISODate('2025-06-11T10:48:01.291Z')"
  }
]

範例 2:依每家商店的銷售金額取得第一個類別

若要擷取每個商店內的第一個類別(依字母順序排列),請執行查詢來排序每個商店內的銷售類別清單,並從每個商店的已排序結果集傳回第一個類別。

db.stores.aggregate([{
        $unwind: "$sales.salesByCategory"
    },
    {
        $sort: {
            "_id": 1,
            "sales.salesByCategory.categoryName": 1
        }
    },
    {
        $group: {
            _id: "$_id",
            storeName: {
                $first: "$name"
            },
            totalSales: {
                $first: "$sales.totalSales"
            },
            firstCategory: {
                $first: {
                    categoryName: "$sales.salesByCategory.categoryName",
                    categoryTotalSales: "$sales.salesByCategory.totalSales"
                }
            }
        }
    }
])

此查詢傳回的前兩個結果是:

[
    {
        "_id": "64ec6589-068a-44a6-be5b-9d37bb0a90f1",
        "storeName": "First Up Consultants | Computer Gallery - West Cathrine",
        "totalSales": 186829,
        "firstCategory": {
            "categoryName": "Cases",
            "categoryTotalSales": 36386
        }
    },
    {
        "_id": "14343900-2a5c-44bf-a52b-9efe63579866",
        "storeName": "Northwind Traders | Home Improvement Closet - West Evanside",
        "totalSales": 35371,
        "firstCategory": {
            "categoryName": "Doors",
            "categoryTotalSales": 21108
        }
    }
]

範例 3:取得每家商店的第一次促銷活動

若要擷取每個商店的第一個促銷事件,請執行查詢,依 startDate 排序每個商店內的促銷事件清單,並從每個存放區排序結果集傳回第一個事件。

根據開始日期取得每家商店的第一次促銷活動。

db.stores.aggregate([{
        $unwind: "$promotionEvents"
    },
    {
        $sort: {
            "_id": 1,
            "promotionEvents.promotionalDates.startDate.Year": 1,
            "promotionEvents.promotionalDates.startDate.Month": 1,
            "promotionEvents.promotionalDates.startDate.Day": 1
        }
    },
    {
        $group: {
            _id: "$_id",
            storeName: {
                $first: "$name"
            },
            firstPromotionEvent: {
                $first: {
                    eventName: "$promotionEvents.eventName",
                    startYear: "$promotionEvents.promotionalDates.startDate.Year",
                    startMonth: "$promotionEvents.promotionalDates.startDate.Month"
                }
            }
        }
    }
])

此查詢傳回的前兩個結果是:

[
    {
        "_id": "64ec6589-068a-44a6-be5b-9d37bb0a90f1",
        "storeName": "First Up Consultants | Computer Gallery - West Cathrine",
        "firstPromotionEvent": {
            "eventName": "Crazy Markdown Madness",
            "startYear": 2024,
            "startMonth": 6
        }
    },
    {
        "_id": "a58d0356-493b-44e6-afab-260aa3296930",
        "storeName": "Fabrikam, Inc. | Outdoor Furniture Nook - West Lexie",
        "firstPromotionEvent": {
            "eventName": "Price Drop Palooza",
            "startYear": 2023,
            "startMonth": 9
        }
    }
]