$maxN演算子は、指定されたフィルター条件に基づいてフィールドの上位 N 個の値を取得するために使用されます。
構文
$maxN: {
input: < field or expression > ,
n: < number of values to retrieve >
}
パラメーター
| パラメーター | Description |
|---|---|
input |
最大値を評価するフィールドまたは式を指定します。 |
n |
取得する最大値の数を指定します。 正の整数にする必要があります。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
}
],
"tag": [
"#ShopLocal",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
],
"company": "Lakeshore Retail",
"city": "Port Cecile",
"lastUpdated": {
"$date": "2024-12-11T10:21:58.274Z"
}
}
例 1: $maxN を accumulator として使用して、上位 2 つの販売カテゴリを取得する
このクエリでは、パフォーマンスの高いカテゴリを含む上位 2 つの販売カテゴリを取得するか、ストア全体で上位のカテゴリを集計します。
db.stores.aggregate([{
$project: {
topSalesCategories: {
$maxN: {
input: "$sales.salesByCategory",
n: 2
}
}
}
},
{
$limit: 4
}
])
このクエリは、次の結果を返します。
[
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"topSalesCategories": [
{
"categoryName": "Stockings",
"totalSales": 25731
}
]
},
{
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
"topSalesCategories": [
{
"categoryName": "Markers",
"totalSales": 3927
}
]
},
{
"_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
"topSalesCategories": [
{
"categoryName": "Storage Boxes",
"totalSales": 2236
}
]
},
{
"_id": "94792a4c-4b03-466b-91f6-821c4a8b2aa4",
"topSalesCategories": [
{
"categoryName": "Travel Backpacks",
"totalSales": 13189
},
{
"categoryName": "Suitcases",
"totalSales": 37858
}
]
}
]
例 2: で $maxN を使用する $setWindowFields
このクエリでは、2023 年の "Laptops" の上位 N 件の割引が都市ごとに取得されます。 このクエリは、2023 年に "Laptops" 割引エントリをフィルター処理し、結果のドキュメントを市区町村別にグループ化します。
db.stores.aggregate([
{ $unwind: "$promotionEvents" },
{ $unwind: "$promotionEvents.discounts" },
{
$match: {
"promotionEvents.discounts.categoryName": "Laptops",
"promotionEvents.promotionalDates.startDate.Year": 2023
}
},
{
$group: {
_id: "$city",
allDiscounts: {
$push: "$promotionEvents.discounts.discountPercentage"
}
}
},
{
$project: {
topDiscounts: {
$slice: [
{ $sortArray: { input: "$allDiscounts", sortBy: -1 } },
3 // Top N discounts
]
}
}
}
])
このクエリによって返される最初の 3 つの結果は次のとおりです。
[
{
"_id": "Lake Margareteland",
"topDiscounts": [
18
]
},
{
"_id": "Horacetown",
"topDiscounts": [
13
]
},
{
"_id": "D'Amoreside",
"topDiscounts": [
9
]
}
]
例 3: 配列式として $maxN 演算子を使用して上位 3 つの売上カテゴリを検索する
このクエリは、すべての販売カテゴリから上位 3 つの売上値を取得します。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
topThreeSales: {
$maxN: {
input: "$sales.salesByCategory.totalSales",
n: 3
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"topThreeSales": [43522, 32272, 28946]
}
]