$dayOfMonth

The $dayOfMonth operator 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

Parameter Description
<dateExpression> The date expression from which to extract the day of the month.

Examples

Let's understand the usage with sample json from stores dataset.

{
  "_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
  "name": "Relecloud | Toy Collection - North Jaylan",
  "location": {
    "lat": 2.0797,
    "lon": -94.4134
  },
  "staff": {
    "employeeCount": {
      "fullTime": 7,
      "partTime": 4
    }
  },
  "sales": {
    "salesByCategory": [
      {
        "categoryName": "Educational Toys",
        "totalSales": 3299
      }
    ],
    "revenue": 3299
  },
  "promotionEvents": [
    {
      "eventName": "Massive Markdown Mania",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 21
        },
        "endDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 29
        }
      },
      "discounts": [
        {
          "categoryName": "Remote Control Toys",
          "discountPercentage": 6
        },
        {
          "categoryName": "Building Sets",
          "discountPercentage": 21
        }
      ]
    }
  ],
  "company": "Relecloud",
  "city": "North Jaylan",
  "lastUpdated": {
    "$timestamp": {
      "t": 1733313006,
      "i": 1
    }
  },
  "storeOpeningDate": "2024-09-05T11:50:06.549Z"
}

Example 1: Extract day of the month

The query uses the $dayOfMonth operator to extract the day of the month (1–31) from the lastUpdated timestamp. It helps in isolating just the date component for reporting or grouping.

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

The result displays the numeric day of the month on which lastUpdated occurred. In current scenario, 4 as the date was "2024-12-04".

{
  "dayOfMonth": "4"
}