$year

Operator $year zwraca rok dla daty jako czterocyfrową liczbę (na przykład 2024). Jeśli data ma wartość null lub jej brakuje, $year zwraca wartość null.

Składnia

Składnia $objectToArray operatora jest następująca:

{
  $year: <dateExpression>
}

Lub ze specyfikacją strefy czasowej

{
  $year: {
    date: <dateExpression>,
    timezone: <timezoneExpression>
  }
}

Parametry

Parameter Description
dateExpression Dowolne wyrażenie rozpoznawane jako Data, Sygnatura czasowa lub ObjectId.
timezone Opcjonalny. Strefa czasowa do użycia na potrzeby obliczeń. Może być identyfikatorem strefy czasowej Olson (na przykład "Ameryka/New_York") lub przesunięciem UTC (na przykład "+0530").

Przykłady

Rozważmy ten przykładowy dokument z kolekcji sklepów.

{
    "_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
                }
            ]
        }
    ]
}

Przykład 1. Wyodrębnianie roku z daty otwarcia sklepu

To zapytanie wyodrębnia rok otwarcia sklepu.

db.stores.aggregate([
  { $match: { _id: "905d1939-e03a-413e-a9c4-221f74055aac" } },
  {
    $project: {
      name: 1,
      storeOpeningDate: 1,
      openingYear: { $year: { $toDate: "$storeOpeningDate" } }
    }
  }
])

To zapytanie zwraca następujący wynik.

[
  {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "storeOpeningDate": "2024-12-30T22:55:25.779Z",
    "openingYear": 2024
  }
]

Przykład 2. Znajdowanie sklepów otwartych w określonym roku

To zapytanie pobiera wszystkie magazyny, które zostały otwarte w 2021 roku.

db.stores.aggregate([
  {
    $match: {
      $expr: {
        $eq: [{ $year: { $toDate: "$storeOpeningDate" } }, 2021]
      }
    }
  },
  {
    $project: {
      name: 1,
      city: 1,
      openingYear: { $year: { $toDate: "$storeOpeningDate" } },
      storeOpeningDate: 1
    }
  }
])

To zapytanie zwraca następujący wynik.

[
  {
    "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
    "city": "South Amir",
    "storeOpeningDate": "2021-10-03T00:00:00.000Z",
    "name": "First Up Consultants | Bed and Bath Center - South Amir",
    "openingYear": 2021
  }
]