0
0
MongoDBquery~10 mins

Date expressions ($year, $month, $dayOfMonth) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date expressions ($year, $month, $dayOfMonth)
Start with Date Field
Extract $year
Use extracted parts
Output Result
Extract year, month, and day from a date field step-by-step to use in queries or transformations.
Execution Sample
MongoDB
db.orders.aggregate([
  { $project: {
      year: { $year: "$orderDate" },
      month: { $month: "$orderDate" },
      day: { $dayOfMonth: "$orderDate" }
  }}
])
This query extracts the year, month, and day from the orderDate field of each document.
Execution Table
StepDocument orderDate$year$month$dayOfMonthOutput Document
12023-06-15T10:30:00Z2023615{"year": 2023, "month": 6, "day": 15}
22022-12-01T08:00:00Z2022121{"year": 2022, "month": 12, "day": 1}
32024-01-31T23:59:59Z2024131{"year": 2024, "month": 1, "day": 31}
42023-11-07T00:00:00Z2023117{"year": 2023, "month": 11, "day": 7}
52023-06-15T10:30:00Z2023615{"year": 2023, "month": 6, "day": 15}
💡 All documents processed, date parts extracted for each.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
orderDateN/A2023-06-15T10:30:00Z2022-12-01T08:00:00Z2024-01-31T23:59:59Z2023-11-07T00:00:00ZProcessed all
$yearN/A2023202220242023Last value 2023
$monthN/A612111Last value 11
$dayOfMonthN/A151317Last value 7
Key Moments - 3 Insights
Why does $month return 6 for June instead of 5?
$month returns the calendar month number starting at 1 for January, so June is 6. See execution_table rows 1 and 5 where $month is 6 for June dates.
What happens if the date field is missing or null?
If the date field is missing or null, $year, $month, and $dayOfMonth return null or no value. This is not shown in the current table but is important to handle in real queries.
Are the extracted values numbers or strings?
The extracted values are numbers, as shown in the output documents in execution_table rows, e.g., "year": 2023 is a number.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the $dayOfMonth value at Step 3?
A7
B31
C1
D15
💡 Hint
Check the $dayOfMonth column in execution_table row with Step 3.
At which step does $month equal 12?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the $month column in execution_table and find where it is 12.
If the orderDate was missing in a document, what would $year return?
ACurrent year
B0
CNull or no value
DError stops query
💡 Hint
Refer to key_moments about missing or null date fields.
Concept Snapshot
Date expressions extract parts from a date field.
$year returns the year number (e.g., 2023).
$month returns the month number (1-12).
$dayOfMonth returns the day number (1-31).
Use in aggregation $project to create new fields.
Handles ISODate or date strings internally.
Full Transcript
This visual execution trace shows how MongoDB date expressions $year, $month, and $dayOfMonth extract parts from a date field in documents. Each step processes one document's orderDate, extracting year, month, and day as numbers. The execution table lists each document's date and the extracted values. The variable tracker shows how these values change per document. Key moments clarify common confusions like month numbering starting at 1, handling missing dates, and data types of extracted parts. The quiz tests understanding of these values at specific steps and behavior with missing dates. The snapshot summarizes usage and behavior of these date expressions in MongoDB aggregation pipelines.