$isoWeekYear
The $isoWeekYear operator in Amazon DocumentDB returns the ISO 8601 week year number for a given date. The ISO week year number differs from the Gregorian calendar year in that the week year can be different from the calendar year, especially at the start and end of the year.
Parameters
-
expression: The date expression for which to return the ISO 8601 week year number.
Example (MongoDB Shell)
This example demonstrates how to use the $isoWeekYear operator to retrieve the ISO 8601 week year for the date field of each document in the events collection.
Create sample documents
db.events.insertMany([ { _id: 1, name: "Event 1", date: ISODate("2022-12-31T00:00:00Z") }, { _id: 2, name: "Event 2", date: ISODate("2023-01-01T00:00:00Z") }, { _id: 3, name: "Event 3", date: ISODate("2023-01-02T00:00:00Z") } ]);
Query example
db.events.aggregate([ { $project: { name: 1, isoWeekYear: { $isoWeekYear: "$date" } }} ]);
Output
[
{ "_id" : 1, "name" : "Event 1", "isoWeekYear" : 2023 },
{ "_id" : 2, "name" : "Event 2", "isoWeekYear" : 2023 },
{ "_id" : 3, "name" : "Event 3", "isoWeekYear" : 2023 }
]
Code examples
To view a code example for using the $isoWeekYear command, choose the tab for the language that you want to use: