$dayOfYear

Extracts the day of the year from a date value, where 1 represents January 1. It's useful for grouping or filtering documents based on the day of the year.

Syntax

$dayOfYear: <dateExpression>

Parameters

dateExpressionobjectrequired

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

Examples

Extract day of the year

This query uses the $dayOfYear operator to extract the ordinal day of the year (1–366) from the lastUpdated timestamp.

Query:

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

Output:

[
  {
    "dayOfYear": 339
  }
]

Related