Step 1: Extract year, month, and day from the date
The date is July 15, 2023. So, year = 2023, month = 7 (July), day = 15.
Step 2: Confirm output matches extracted values
The output document will have year: 2023, month: 7, day: 15 as projected.
Final Answer:
{ year: 2023, month: 7, day: 15 } -> Option C
Quick Check:
Date parts match output [OK]
Hint: Remember months are 1-based in MongoDB date expressions [OK]
Common Mistakes:
Assuming months start at 0 instead of 1
Confusing dayOfMonth with dayOfWeek
Misreading the date value
4. You wrote this aggregation stage to get the day of the month from orderDate:
{ $project: { day: { $dayOfMonth: orderDate } } }
But it throws an error. What is the mistake?
medium
A. Missing quotes and $ before field name in $dayOfMonth
B. Using $project instead of $match
C. Field orderDate is not a date type
D. Incorrect operator; should use $day instead
Solution
Step 1: Check field reference syntax inside $dayOfMonth
Field names must be referenced as strings with a $ prefix, so it should be "$orderDate".
Step 2: Identify the cause of error
Using orderDate without quotes and $ causes a syntax error in aggregation.
Final Answer:
Missing quotes and $ before field name in $dayOfMonth -> Option A
Quick Check:
Field names need "$" prefix in aggregation [OK]
Hint: Always use "$fieldName" inside aggregation expressions [OK]
Common Mistakes:
Forgetting $ and quotes around field names
Assuming $day is a valid operator
Confusing aggregation stages
5. You want to group sales documents by the year and month of their saleDate field. Which aggregation pipeline stage correctly groups by year and month using date expressions?