$dayOfWeek

Extracts the day of the week from a date value, where 1 represents Sunday and 7 represents Saturday. It's useful for grouping or filtering documents based on the day of the week.

Syntax

$dayOfWeek: <dateExpression>

Parameters

dateExpressionobjectrequired

The date expression from which to extract the day of the week.

Examples

Extract day of the week

This query uses the $dayOfWeek operator to extract the day of the week from the lastUpdated timestamp. The returned value ranges from 1 (Sunday) to 7 (Saturday), based on ISO-8601 ordering.

Query:

db.stores.aggregate([
  {
    $match: { _id: "e6410bb3-843d-4fa6-8c70-7472925f6d0a" }
  },
  {
    $project: {
      _id: 0,
      dayOfWeek: { $dayOfWeek: "$lastUpdated" }
    }
  }
])

Output:

[
  {
    "dayOfWeek": "4"
  }
]

Related