$documentNumber 运算符根据分区内的一个或多个字段对文档进行排序,并为结果集中的每个文档分配一个文档编号。
Syntax
{
$documentNumber: {}
}
例子
请考虑商店集合中的这个示例文档。
{
"_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 公司旗下每家商店的文档编号(位置排名),首先运行查询以根据公司名称进行筛选,然后按总销售额的升序对结果进行排序,并为排序结果集中的每个文档分配一个文档编号。
db.stores.aggregate([{
"$match": {
"company": {
"$in": ["First Up Consultants"]
}
}
}, {
"$setWindowFields": {
"partitionBy": "$company",
"sortBy": {
"sales.totalSales": -1
},
"output": {
"documentNumber": {
"$documentNumber": {}
}
}
}
}, {
"$project": {
"company": 1,
"documentNumber": 1
}
}])
此查询返回的前 5 个结果为:
[
{
"_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
"company": "First Up Consultants",
"documentNumber": 1
},
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"company": "First Up Consultants",
"documentNumber": 2
},
{
"_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
"company": "First Up Consultants",
"documentNumber": 3
},
{
"_id": "bfb213fa-8db8-419f-8e5b-e7096120bad2",
"company": "First Up Consultants",
"documentNumber": 4
},
{
"_id": "14ab145b-0819-4d22-9e02-9ae0725fcda9",
"company": "First Up Consultants",
"documentNumber": 5
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。