運算子會將 $dateAdd 指定的時間單位數目新增至日期。 在您需要根據指定日期和時間間隔計算未來日期的案例中,這非常有用。
語法
$dateAdd: {
startDate: <expression>,
unit: <string>,
amount: <number>,
timezone: <string> // Optional
}
參數
| 參數 | Description |
|---|---|
startDate |
加法作業的開始日期。 |
unit |
要加入的時間單位。 有效單位包括:year、、、quartermonth、、 。weekdayhourminutesecondmillisecond |
amount |
要加入的單位數目。 |
timezone |
選擇性。 要用於作業的時區。 |
範例
請參考商店集合中的此範例檔。
{
"_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 和計算 a 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:將月份新增至日期
此彙總查詢會投影 , eventName 並計算 newStartDate,方法是將 1 個月新增至巢狀升級欄位的重建開始日期。 這有助於根據原始排程來判斷調整的事件開始日期。 此查詢會從巢狀促銷事件資料傳回每個文件的 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 的功能相容性的更多資訊。