$dateToParts

運算符 $dateToParts 可用來從日期物件擷取個別元件(Year、Month、Day、Hour、Minute、Second、Millisecond 等等)。 運算子適用於需要作或分析特定日期元件的案例,例如根據個別日期元件排序、篩選或匯總數據。

語法

$dateToParts: {
  date: <dateExpression>,
  timezone: <string>, // optional
  iso8601: <boolean> // optional
}

參數

參數 Description
date 要從中擷取元件的日期表達式。
timezone 選擇性。 指定日期的時區。 如果未提供,則預設為UTC。
iso8601 選擇性。 如果為 true,運算符會使用 ISO 8601 週日期行事曆系統。 預設為 False。

範例

請參考商店集合中的此範例檔。

{
    "_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:從欄位擷取日期元件

此查詢用於 $dateToParts 將日期分解 lastUpdated 為年、月、日和時間等元件。 它有助於分析或轉換日期的各個部分,以便進一步處理。

db.stores.aggregate([
  {
    $match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
  },
  {
    $project: {
      _id: 0,
      dateParts: {
        $dateToParts: { 
          date: "$lastUpdated" 
        }
      }
    }
  }
])

此查詢會傳回下列結果。

[
  {
    "dateParts": {
      "year": 2024,
      "month": 12,
      "day": 4,
      "hour": 11,
      "minute": 50,
      "second": 6,
      "millisecond": 0
    }
  }
]

範例 2:使用時區

此查詢會 lastUpdated 擷取特定文件的時間戳記,並使用 $dateToParts 將其分解為日期部分,例如年、月、日和小時。 包括「美國/New_York」時區允許分解,反映當地時間,而不是UTC。

db.stores.aggregate([
  {
    $match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
  },
  {
    $project: {
      _id: 0,
      datePartsWithTimezone: {
        $dateToParts: { 
          date: "$lastUpdated", 
          timezone: "America/New_York" 
        }
      }
    }
  }
])

此查詢會傳回下列結果。

[
  {
    "datePartsWithTimezone": {
      "year": 2024,
      "month": 12,
      "day": 4,
      "hour": 6,
      "minute": 50,
      "second": 6,
      "millisecond": 0
    }
  }
]