다음을 통해 공유


$dateFromParts

연산자는 $dateFromParts 연도, 월, 일, 시간, 분, 초 및 밀리초와 같은 개별 구성 요소에서 날짜를 생성합니다. 이 연산자는 날짜 구성 요소를 별도로 저장하는 데이터를 처리할 때 유용할 수 있습니다.

문법

{
    $dateFromParts: {
        year: < year > ,
        month: < month > ,
        day: < day > ,
        hour: < hour > ,
        minute: < minute > ,
        second: < second > ,
        millisecond: < millisecond > ,
        timezone: < timezone >
    }
}

매개 변수

매개 변수 Description
year 날짜의 연도 구성 요소입니다.
month 날짜의 월 구성 요소입니다.
day 날짜의 일 구성 요소입니다.
hour 날짜의 시간 구성 요소입니다.
minute 날짜의 분 구성 요소입니다.
second 날짜의 두 번째 구성 요소입니다.
millisecond 날짜의 밀리초 구성 요소입니다.
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: 시작 날짜 생성

이 쿼리는 중첩된 필드에서 $dateFromParts정확한 startDate 및 endDate 값을 생성한 다음, 이벤트 기간을 일 단위로 계산합니다. 조각화된 날짜 형식으로 저장된 이벤트 타임라인을 표준화하고 분석하는 데 도움이 됩니다.

db.stores.aggregate([
  { 
    $match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" } 
  },
  { 
    $unwind: "$promotionEvents" 
  },
  {
    $project: {
      _id: 1,
      startDate: {
        $dateFromParts: {
          year: "$promotionEvents.promotionalDates.startDate.Year",
          month: "$promotionEvents.promotionalDates.startDate.Month",
          day: "$promotionEvents.promotionalDates.startDate.Day"
        }
      }
    }
  }
])

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
    "startDate": "2024-09-21T00:00:00.000Z"
  }
]