$dateToParts

$dateToParts演算子は、日付オブジェクトから個々のコンポーネント (Year、Month、Day、Hour、Minute、Second、Millisecond など) を抽出するために使用されます。 この演算子は、個々の日付コンポーネントに基づくデータの並べ替え、フィルター処理、集計など、特定の日付部分の操作や分析が必要なシナリオに役立ちます。

構文

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

パラメーター

パラメーター Description
date パーツを抽出する日付式。
timezone Optional. 日付のタイムゾーンを指定します。 指定しない場合、既定値は UTC です。
iso8601 Optional. true の場合、オペレーターは ISO 8601 週日付カレンダー システムを使用します。 既定は false です。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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を使用して年、月、日、時間などの日付部分に分割します。 "America/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
    }
  }
]