Navigation

$isoWeek (aggregation)

On this page

Definition

$isoWeek

New in version 3.4.

Returns the week number in ISO 8601 format, ranging from 1 to 53. Week numbers start at 1 with the week (Monday through Sunday) that contains the year’s first Thursday.

$isoWeek has the following operator expression syntax:

{ $isoWeek: <date expression> }

The argument can be any valid expression that resolves to a BSON ISODate object, a BSON Timestamp object, or a Date object.

Behavior

Example Result
{ $isoWeek: new Date("2016-01-01") } 53
{ $isoWeek: new Date("2015-01-01") } 1
{ $isoWeek: new Date("August 14, 2011") } 32
{ $isoWeek: ISODate("1998-11-07T00:00:00Z") } 45
{ $isoWeek: "March 28, 1976" } error
{ $isoWeek: "2009-04-09" } error

Note

$isoWeek cannot take a string as an argument.

Example

A collection called deliveries contains the following documents:

{ "_id" : 1, "date" : ISODate("2006-10-24T00:00:00Z"), "city" : "Boston" }
{ "_id" : 2, "date" : ISODate("2011-08-18T00:00:00Z"), "city" : "Detroit" }

The following operation returns the week number for each date field.

db.deliveries.aggregate( [
  {
    $project: {
      _id: 0,
      city: "$city",
      weekNumber: { $isoWeek: "$date" }
    }
  }
] )

The operation returns the following results:

{ "city" : "Boston", "weekNumber" : 43 }
{ "city" : "Detroit", "weekNumber" : 33 }