$isoWeekYear - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const events = db.collection('events'); const result = await events.aggregate([ { $project: { name: 1, isoWeekYear: { $isoWeekYear: '$date' } }} ]).toArray(); console.log(result); client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test events = db.events result = list(events.aggregate([ { '$project': { 'name': 1, 'isoWeekYear': { '$isoWeekYear': '$date' } }} ])) print(result) client.close() example()