0
0
MongoDBquery~5 mins

Date expressions ($year, $month, $dayOfMonth) in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $year date expression do in MongoDB?
The $year expression extracts the year part from a date value in MongoDB documents.
Click to reveal answer
beginner
How does $month work in MongoDB date expressions?
The $month expression extracts the month (1-12) from a date value in MongoDB documents.
Click to reveal answer
beginner
What value range does $dayOfMonth return in MongoDB?
$dayOfMonth returns the day of the month as a number from 1 to 31, based on the date value.
Click to reveal answer
intermediate
Write a MongoDB aggregation snippet to get the year from a field called createdAt.
Use { $year: "$createdAt" } inside a $project or $addFields stage to extract the year.
Click to reveal answer
intermediate
Why are date expressions like $year, $month, and $dayOfMonth useful in MongoDB?
They help break down dates into parts for filtering, grouping, or formatting data easily in aggregation pipelines.
Click to reveal answer
What does { $month: "$dateField" } return in MongoDB?
AThe month number (1-12) from the dateField
BThe day of the week from the dateField
CThe year from the dateField
DThe full date as a string
Which expression extracts the day of the month from a date in MongoDB?
A<code>$dayOfMonth</code>
B<code>$dateToString</code>
C<code>$dayOfYear</code>
D<code>$dayOfWeek</code>
If a date is '2024-06-15', what will { $year: "$date" } return?
A6
B2024
C15
DJune
Which MongoDB stage commonly uses $year, $month, and $dayOfMonth?
A<code>$match</code>
B<code>$group</code>
C<code>$project</code>
DAll of the above
What is the valid range of values returned by $month in MongoDB?
A0 to 11
B1 to 31
C1 to 12
D0 to 23
Explain how to extract the year, month, and day from a date field in MongoDB aggregation.
Think about breaking down a date into parts using MongoDB expressions.
You got /4 concepts.
    Describe a real-life scenario where extracting year, month, and day from dates in MongoDB would be helpful.
    Consider how businesses analyze data over time.
    You got /4 concepts.