聚合管道中的 $sort 阶段用于按指定的字段对管道中的文档排序。 此阶段可帮助你对数据进行排序,例如按金额或按日期来排列销售额。
语法
{
$sort: {
< field1 >: < sort order > ,
< field2 >: < sort order >
}
}
参数
参数 | DESCRIPTION |
---|---|
field |
排序依据的字段 |
sort order |
应该用来对字段进行排序的顺序。 1 表示升序,-1 表示降序。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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:按升序按总销售额排序
按总销售额采用降序对销售类别排序:
db.collection.aggregate([
{
$unwind: "$store.sales.salesByCategory"
},
{
$sort: { "store.sales.salesByCategory.totalSales": -1 }
}
])
此查询返回的前两个结果为:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"sales": {
"salesByCategory": [
{
"category": "Electronics",
"totalSales": 15000
},
{
"category": "Clothing",
"totalSales": 12000
},
{
"category": "Home Goods",
"totalSales": 10000
}
]
}
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c6",
"store": {
"name": "Uptown Store",
"sales": {
"salesByCategory": [
{
"category": "Electronics",
"totalSales": 20000
},
{
"category": "Clothing",
"totalSales": 18000
},
{
"category": "Home Goods",
"totalSales": 15000
}
]
}
}
}
]
示例 2:按事件开始日期按升序排序
按开始日期采用升序对促销活动排序:
db.collection.aggregate([
{
$unwind: "$store.promotionEvents"
},
{
$sort: { "store.promotionEvents.promotionalDates.startDate": 1 }
}
])
此查询返回的前两个结果为:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": [
{
"eventName": "Summer Sale",
"promotionalDates": {
"startDate": "2024-06-01T00:00:00Z",
"endDate": "2024-06-30T23:59:59Z"
}
},
{
"eventName": "Black Friday",
"promotionalDates": {
"startDate": "2024-11-25T00:00:00Z",
"endDate": "2024-11-25T23:59:59Z"
}
},
{
"eventName": "Holiday Deals",
"promotionalDates": {
"startDate": "2024-12-01T00:00:00Z",
"endDate": "2024-12-31T23:59:59Z"
}
}
]
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c6",
"store": {
"name": "Uptown Store",
"promotionEvents": [
{
"eventName": "Back to School",
"promotionalDates": {
"startDate": "2024-08-01T00:00:00Z",
"endDate": "2024-08-31T23:59:59Z"
}
},
{
"eventName": "Winter Sale",
"promotionalDates": {
"startDate": "2024-12-01T00:00:00Z",
"endDate": "2024-12-31T23:59:59Z"
}
}
]
}
}
]
示例 3:对对象数组进行排序
该示例根据 salesByCategory
字段按升序对 totalSales
数组进行排序。
db.stores.updateOne({
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
$push: {
"sales.salesByCategory": {
$each: [],
$sort: {
totalSales: 1
}
}
}
}
)
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。