$isoWeekYear (ISO 주간 연도)

연산자는 $isoWeekYear ISO 8601 형식으로 연도 번호를 반환합니다. ISO 주 번호 매기기 연도는 연초 또는 끝 날짜의 연도와 다를 수 있습니다. ISO 주 연도는 해당 주의 목요일을 포함하는 해입니다.

문법

{
  $isoWeekYear: <dateExpression>
}

매개 변수

매개 변수 Description
dateExpression Date, Timestamp 또는 ObjectId로 확인되는 식입니다. 식이 null로 확인되거나 누락 $isoWeekYear 된 경우 null을 반환합니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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: 역년 및 ISO 주 연도 비교

이 쿼리는 특히 연도 경계에 가까운 날짜에 대해 역년과 ISO 주 연도의 차이를 보여 줍니다.

db.stores.aggregate([
  { $match: {_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
  { $unwind: "$promotionEvents" },
  {
    $project: {
      eventName: "$promotionEvents.eventName",
      startDate: {
        $dateFromParts: {
          year: "$promotionEvents.promotionalDates.startDate.Year",
          month: "$promotionEvents.promotionalDates.startDate.Month",
          day: "$promotionEvents.promotionalDates.startDate.Day"
        }
      },
      endDate: {
        $dateFromParts: {
          year: "$promotionEvents.promotionalDates.endDate.Year",
          month: "$promotionEvents.promotionalDates.endDate.Month",
          day: "$promotionEvents.promotionalDates.endDate.Day"
        }
      }
    }
  },
  {
    $project: {
      eventName: 1,
      startDate: 1,
      endDate: 1,
      startCalendarYear: { $year: "$startDate" },
      startISOWeekYear: { $isoWeekYear: "$startDate" },
      endCalendarYear: { $year: "$endDate" },
      endISOWeekYear: { $isoWeekYear: "$endDate" },
      yearDifference: {
        $ne: [{ $year: "$startDate" }, { $isoWeekYear: "$startDate" }]
      }
    }
  },
  { $match: {"eventName": "Discount Delight Days" } }
])

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

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "eventName": "Discount Delight Days",
    "startDate": "2023-12-26T00:00:00.000Z",
    "endDate": "2024-01-05T00:00:00.000Z",
    "startCalendarYear": 2023,
    "startISOWeekYear": Long("2023"),
    "endCalendarYear": 2024,
    "endISOWeekYear": Long("2024"),
    "yearDifference": true
  }
]