$topN演算子は、クエリで指定された 1 つ以上のフィールドのドキュメントを並べ替え、フィルター条件に一致する最初の N 個のドキュメントを返します。 これは $top の機能を拡張するものです。最上位の 1 要素だけでなく、複数の上位要素を取得できます。
構文
{
$topN: {
output: [listOfFields],
sortBy: {
<fieldName>: < sortOrder >
},
n: < numDocumentsToReturn >
}
}
パラメーター
| パラメーター | Description |
|---|---|
listOfFields |
結果セット内の最後のドキュメントに対して返されるフィールドの一覧 |
fieldName |
結果セットの並べ替えに使用するフィールド |
sortOrder |
1 または -1。 1 はフィールドの値の昇順で並べ替えを意味し、-1 はフィールドの値の降順で並べ替えを意味します |
n |
並べ替えられた結果セットの先頭から返されるドキュメントの数 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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 - 売上合計が最も低い 2 つのストアを取得する
First Up Consultants 企業内の売上別の 2 つの最下位ストアを取得するには、クエリを実行して会社名をフィルター処理し、結果のドキュメントを売上の昇順で並べ替え、並べ替えられた結果セットから上位 2 つのドキュメントを返します。
db.stores.aggregate([{
$match: {
company: {
$in: ["First Up Consultants"]
}
}
}, {
$group: {
_id: "$company",
topSales: {
$topN: {
output: ["$company", "$sales"],
sortBy: {
"sales.totalSales": 1
},
n: 2
}
}
}
}])
このクエリは、次の結果を返します。
[
{
"_id": "First Up Consultants",
"topSales": [
[
"First Up Consultants",
{
"salesByCategory": [
{
"categoryName": "Towel Sets",
"totalSales": 520
},
{
"categoryName": "Bath Accessories",
"totalSales": 41710
},
{
"categoryName": "Drapes",
"totalSales": 42893
},
{
"categoryName": "Towel Racks",
"totalSales": 30773
},
{
"categoryName": "Hybrid Mattresses",
"totalSales": 39491
},
{
"categoryName": "Innerspring Mattresses",
"totalSales": 6410
},
{
"categoryName": "Bed Frames",
"totalSales": 41917
},
{
"categoryName": "Mattress Protectors",
"totalSales": 44124
},
{
"categoryName": "Bath Towels",
"totalSales": 5671
},
{
"categoryName": "Turkish Towels",
"totalSales": 25674
}
],
"revenue": 279183
}
],
[
"First Up Consultants",
{
"salesByCategory": [
{
"categoryName": "Lavalier Microphones",
"totalSales": 40000
},
{
"categoryName": "Wireless Microphones",
"totalSales": 39691
}
],
"minimumSalesTarget": 30000,
"revenue": 50000
}
]
]
}
]
例 2: 最新の 2 つのプロモーション イベントを取得する
各ストアの最新の 2 つのプロモーション イベントを見つけるには、コレクション内のドキュメントをストア別にグループ化し、昇格日の昇順で並べ替え、ストアごとに並べ替えられた結果セットから上位 2 つの結果を返します。
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$group: {
_id: "$_id",
storeName: {
$first: "$name"
},
top2RecentPromotions: {
$topN: {
n: 2,
sortBy: {
"promotionEvents.promotionalDates.startDate.Year": -1,
"promotionEvents.promotionalDates.startDate.Month": -1,
"promotionEvents.promotionalDates.startDate.Day": -1
},
output: {
eventName: "$promotionEvents.eventName",
startDate: "$promotionEvents.promotionalDates.startDate"
}
}
}
}
}
])
このクエリによって返される最初の 2 つの結果は次のとおりです。
[
{
"_id": "4a99546f-a1d2-4e61-ae9f-b8c7c1faf73c'",
"storeName": "Lakeshore Retail | Stationery Nook - West Van",
"top2RecentPromotions": [
{
"eventName": "Crazy Markdown Madness",
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
}
},
{
"eventName": "Flash Sale Fiesta",
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
}
}
]
},
{
"_id": "e0c47a06-4fe0-46b7-a309-8971bbb3978f",
"storeName": "VanArsdel, Ltd. | Baby Products Bargains - Elainamouth",
"top2RecentPromotions": [
{
"eventName": "Crazy Deal Days",
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
}
}
]
}
]