0
0
MongoDBquery~20 mins

Date expressions ($year, $month, $dayOfMonth) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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: ??? } }
])
A{ year: { $year: "$date" } }
B{ year: { $dayOfMonth: "$date" } }
C{ year: { $month: "$date" } }
D{ year: { $dateToString: { format: "%Y", date: "$date" } } }
Attempts:
2 left
💡 Hint
Use the $year operator to get the year part of a date.
query_result
intermediate
2: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: { ??? } }
])
A{ $expr: { $eq: [ { $month: "$orderDate" }, 3 ] } }
B{ orderDate: { $eq: 3 } }
C{ $expr: { $eq: [ { $dayOfMonth: "$orderDate" }, 3 ] } }
D{ orderDate: { $month: 3 } }
Attempts:
2 left
💡 Hint
Use $expr to compare the month part of the date.
📝 Syntax
advanced
2: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: ??? } }
])
A} } "tAdetaerc$" :htnoMfOyad$ { :yad {
B{ day: { $dayOfMonth: ["$createdAt"] } }
C{ day: { $dayOfMonth: "$createdAt" } }
D{ day: { $dayOfMonth: createdAt } }
Attempts:
2 left
💡 Hint
Field names must be strings with $ prefix inside quotes.
🧠 Conceptual
advanced
2: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?
ADate object representing the extracted part
BString representing the year, month, or day
CInteger number representing the year, month, or day
DBoolean indicating if the date part exists
Attempts:
2 left
💡 Hint
Think about what kind of value you get when you ask for a year or month number.
query_result
expert
3: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: ??? } }
])
A
{ $concat: [
  { $toString: { $year: "$timestamp" } }, "-",
  { $toString: { $month: "$timestamp" } }, "-",
  { $toString: { $dayOfMonth: "$timestamp" } }
] }
B
{ $concat: [
  { $toString: { $year: "$timestamp" } }, "-",
  { $cond: [ { $lt: [ { $month: "$timestamp" }, 10 ] }, { $concat: ["0", { $toString: { $month: "$timestamp" } }] }, { $toString: { $month: "$timestamp" } } ] }, "-",
  { $cond: [ { $lt: [ { $dayOfMonth: "$timestamp" }, 10 ] }, { $concat: ["0", { $toString: { $dayOfMonth: "$timestamp" } }] }, { $toString: { $dayOfMonth: "$timestamp" } } ] }
] }
C{ $dateToString: { format: "%Y-%m-%d", date: "$timestamp" } }
D
{ $concat: [
  { $year: "$timestamp" }, "-",
  { $month: "$timestamp" }, "-",
  { $dayOfMonth: "$timestamp" }
] }
Attempts:
2 left
💡 Hint
You need to add leading zeros for month and day if less than 10.