$dayOfMonth

Extracts the day of the month (1–31) from a date value. It's useful for grouping or filtering documents based on the day of the month.

Syntax

$dayOfMonth: <dateExpression>

Parameters

dateExpressionobjectrequired

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

Examples

Extract day of the month

This query uses the $dayOfMonth operator to extract the day of the month (1–31) from the lastUpdated timestamp and isolates the date component of the field for reporting or grouping.

Query:

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

Output:

[
  {
    "dayOfMonth": "4"
  }
]

Related