$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 顾问公司下最近更新的存储区,请运行查询以提取属于“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
}
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。