연산자는 $dateAdd 날짜에 지정된 시간 단위 수를 추가합니다. 지정된 날짜 및 시간 간격에 따라 이후 날짜를 계산해야 하는 시나리오에서 유용합니다.
문법
$dateAdd: {
startDate: <expression>,
unit: <string>,
amount: <number>,
timezone: <string> // Optional
}
매개 변수
| 매개 변수 | Description |
|---|---|
startDate |
추가 작업의 시작 날짜입니다. |
unit |
추가할 시간 단위입니다. 유효한 단위는 다음을 yearquartermonthweekdayhourminutesecond포함합니다. millisecond |
amount |
추가할 단위 수입니다. |
timezone |
Optional. 작업에 사용할 표준 시간대입니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_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: 날짜에 일 추가
이 쿼리는 중첩된 연도, 월 및 일 필드에서 생성된 날짜에 7일을 추가하여 프로젝트를 실행하고 eventName 계산 newEndDate 합니다. 결과는 이벤트 이름과 확장 종료 날짜를 보여 주는 간소화된 문서입니다.
db.stores.aggregate([
{ $match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" } },
{ $unwind: "$promotionEvents" },
{ $unwind: "$promotionEvents.promotionalDates" },
{
$project: {
eventName: 1,
newEndDate: {
$dateAdd: {
startDate: {
$dateFromParts: {
year: "$promotionEvents.promotionalDates.endDate.Year",
month: "$promotionEvents.promotionalDates.endDate.Month",
day: "$promotionEvents.promotionalDates.endDate.Day"
}
},
unit: "day",
amount: 7
}
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"newEndDate": "2024-10-06T00:00:00.000Z"
}
]
예제 2: 날짜에 월 추가
이 집계 쿼리는 중첩된 승격 필드에서 재구성된 시작 날짜에 1개월을 추가하여 newStartDate를 프로젝 eventName 션하고 계산합니다. 원래 일정에 따라 조정된 이벤트 시작 날짜를 결정하는 데 도움이 됩니다. 이 쿼리는 각 문서의 eventName과 중첩된 승격 이벤트 데이터에서 원래 startDate 이후 1개월 후인 newStartDate를 반환합니다.
db.stores.aggregate([
{ $match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" } },
{ $unwind: "$promotionEvents" },
{ $unwind: "$promotionEvents.promotionalDates" },
{
$project: {
eventName: "$promotionEvents.eventName",
newStartDate: {
$dateAdd: {
startDate: {
$dateFromParts: {
year: "$promotionEvents.promotionalDates.startDate.Year",
month: "$promotionEvents.promotionalDates.startDate.Month",
day: "$promotionEvents.promotionalDates.startDate.Day"
}
},
unit: "month",
amount: 1
}
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"eventName": "Massive Markdown Mania",
"newStartDate": "2024-10-21T00:00:00.000Z"
}
]
관련 콘텐츠
- MongoDB에서 Azure DocumentDB로 마이그레이션하기 위한 옵션을 검토합니다.
- MongoDB와의 기능 호환성에 대해 자세히 알아보세요.