Challenge - 5 Problems
Date Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Extract the year from a date field
Given a MongoDB collection events with documents containing a
date field, which aggregation pipeline stage correctly extracts the year from the date field into a new field called year?MongoDB
db.events.aggregate([
{ $project: { year: ??? } }
])Attempts:
2 left
💡 Hint
Use the $year operator to get the year part of a date.
✗ Incorrect
The $year operator extracts the year from a date. $month and $dayOfMonth extract other parts, and $dateToString returns a string, not a number.
❓ query_result
intermediate2:00remaining
Find documents from a specific month
You want to find all documents in the
orders collection where the orderDate is in March (month 3). Which aggregation pipeline stage filters documents correctly?MongoDB
db.orders.aggregate([
{ $match: { ??? } }
])Attempts:
2 left
💡 Hint
Use $expr to compare the month part of the date.
✗ Incorrect
Only option A uses $expr with $month to compare the month number correctly. Others are invalid or compare wrong parts.
📝 Syntax
advanced2:00remaining
Identify the syntax error in date extraction
Which option contains a syntax error when trying to extract the day of the month from a
createdAt field in an aggregation pipeline?MongoDB
db.users.aggregate([
{ $project: { day: ??? } }
])Attempts:
2 left
💡 Hint
Field names must be strings with $ prefix inside quotes.
✗ Incorrect
Option D misses quotes around "$createdAt", causing a syntax error. Options A and C are correct syntax. Option D uses an array which is invalid for $dayOfMonth.
🧠 Conceptual
advanced2:00remaining
Understanding output types of date expressions
What is the data type of the value returned by the MongoDB $year, $month, and $dayOfMonth expressions when used in an aggregation pipeline?
Attempts:
2 left
💡 Hint
Think about what kind of value you get when you ask for a year or month number.
✗ Incorrect
These expressions return integer numbers, for example, 2024 for year, 6 for June month, or 15 for day 15.
❓ query_result
expert3:00remaining
Combine $year, $month, and $dayOfMonth to format a date string
You want to create a new field
formattedDate in the format YYYY-MM-DD using $year, $month, and $dayOfMonth from a timestamp field. Which aggregation pipeline stage produces the correct string format with leading zeros for month and day?MongoDB
db.logs.aggregate([
{ $project: { formattedDate: ??? } }
])Attempts:
2 left
💡 Hint
You need to add leading zeros for month and day if less than 10.
✗ Incorrect
Option B uses $cond to check if month or day is less than 10 and adds a leading zero, then concatenates all parts as strings. Option B misses leading zeros. Option B uses $dateToString which is simpler but does not use $year, $month, $dayOfMonth as requested. Option B tries to concat numbers directly causing error.