$integral 연산자는 특정 필드를 기준으로 정렬된 지정된 문서 범위에 따라 곡선 아래의 영역을 계산합니다.
문법
{
$integral: {
input: < expression > ,
unit: < time window >
}
}
매개 변수
| 매개 변수 | Description |
|---|---|
input |
정수에 대한 문서의 필드입니다. |
unit |
정수 계열의 지정된 시간 단위입니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_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 컨설턴트 회사의 모든 매장에 대한 총 매출의 정수를 계산하려면 먼저 쿼리를 실행하여 회사 이름을 필터링합니다. 그런 다음, 결과 저장소를 여는 날짜의 오름차순으로 정렬합니다. 마지막으로 정렬된 결과 집합의 첫 번째 문서에서 현재 문서까지 총 판매액의 정수를 계산합니다.
db.stores.aggregate(
[{
"$match": {
"company": {
"$in": [
"First Up Consultants"
]
}
}
},
{
"$setWindowFields": {
"partitionBy": "$company",
"sortBy": {
"storeOpeningDate": 1
},
"output": {
"salesIntegral": {
"$integral": {
"input": "$sales.revenue",
"unit": "hour"
},
"window": {
"range": [
"unbounded",
"current"
],
"unit": "hour"
}
}
}
}
},
{
"$project": {
"company": 1,
"name": 1,
"sales.revenue": 1,
"storeOpeningDate": 1,
"salesIntegral": 1
}
}])
이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.
[
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"sales": {
"revenue": 37701
},
"company": "First Up Consultants",
"storeOpeningDate": "2021-10-03T00:00:00.000Z",
"name": "First Up Consultants | Bed and Bath Center - South Amir",
"salesIntegral": 0
},
{
"_id": "8e7a259b-f7d6-4ec5-a521-3bed53adc587",
"name": "First Up Consultants | Drone Stop - Lake Joana",
"sales": {
"revenue": 14329
},
"company": "First Up Consultants",
"storeOpeningDate": "2024-09-02T00:05:39.311Z",
"salesIntegral": 664945851.9932402
}
]