$maxN 연산자는 지정된 필터링 조건에 따라 필드의 상위 N 값을 검색하는 데 사용됩니다.
문법
$maxN: {
input: < field or expression > ,
n: < number of values to retrieve >
}
매개 변수
| 매개 변수 | Description |
|---|---|
input |
최대값을 계산할 필드 또는 식을 지정합니다. |
n |
검색할 최대값의 수를 지정합니다. 양의 정수여야 합니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_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: 상위 두 개의 판매 범주를 검색하는 데 사용 $maxNaccumulator
이 쿼리는 성과가 높은 범주를 사용하여 상위 두 개의 판매 범주를 검색하거나 매장 전체에서 상위 범주를 집계합니다.
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년 "노트북"에 대한 상위 N개 할인을 검색합니다. 이 쿼리는 2023년에 "노트북" 할인 항목을 필터링한 다음 결과 문서를 도시별로 그룹화합니다.
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
]
}
}
}
])
이 쿼리에서 반환된 처음 세 가지 결과는 다음과 같습니다.
[
{
"_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]
}
]