11 Topics
Chapter•22 Minutes

Cheat Sheet: Date and Time Operators

Date extraction, arithmetic, truncation, parsing, formatting, and system variables.

$year / $month / $dayOfMonth

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Extracts calendar components; timezone affects local calendar boundaries.

Syntax

Date-part expressions with optional timezone

Use case

Group/display by local date.

$year / $month / $dayOfMonth: practical example
{ $project: { year: { $year: { date: "$createdAt", timezone: "Asia/Kolkata" } }, month: { $month: "$createdAt" } } }
💡 Explanation Extracts calendar components; timezone affects local calendar boundaries. When to use it Group/display by local date.
Key Takeaways
  • Extracts calendar components; timezone affects local calendar boundaries.
  • Typical use: Group/display by local date.

$hour / $minute / $second / $millisecond

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Extracts time components; without timezone, date-part operators use UTC.

Syntax

Date-part expressions

Use case

Analyze activity by hour.

$hour / $minute / $second / $millisecond: practical example
{ $project: { hourUtc: { $hour: "$createdAt" } } }
💡 Explanation Extracts time components; without timezone, date-part operators use UTC. When to use it Analyze activity by hour.
Key Takeaways
  • Extracts time components; without timezone, date-part operators use UTC.
  • Typical use: Analyze activity by hour.

$dayOfWeek / $dayOfYear / $isoWeek / $isoWeekYear

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Extracts weekday, day-of-year, or ISO week data.

Syntax

Date-part expressions

Use case

Weekly reports.

Important notes

ISO week-year may differ from calendar year near New Year.

$dayOfWeek / $dayOfYear / $isoWeek / $isoWeekYear: practical example
{ $project: { week: { $isoWeek: "$createdAt" }, isoYear: { $isoWeekYear: "$createdAt" } } }
💡 Explanation Extracts weekday, day-of-year, or ISO week data. When to use it Weekly reports. Caution ISO week-year may differ from calendar year near New Year.
Key Takeaways
  • Extracts weekday, day-of-year, or ISO week data.
  • Typical use: Weekly reports.

$dateAdd

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Adds a date/time unit.

Syntax

{$dateAdd: { startDate, unit, amount, timezone? }}

Use case

Due dates and expiration times.

$dateAdd: practical example
{ $set: { dueAt: { $dateAdd: { startDate: "$createdAt", unit: "day", amount: 7 } } } }
💡 Explanation Adds a date/time unit. When to use it Due dates and expiration times.
Key Takeaways
  • Adds a date/time unit.
  • Typical use: Due dates and expiration times.

$dateSubtract

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Subtracts a date/time unit.

Syntax

{$dateSubtract: { startDate, unit, amount, timezone? }}

Use case

Rolling windows.

$dateSubtract: practical example
{ $set: { windowStart: { $dateSubtract: { startDate: "$$NOW", unit: "day", amount: 30 } } } }
💡 Explanation Subtracts a date/time unit. When to use it Rolling windows.
Key Takeaways
  • Subtracts a date/time unit.
  • Typical use: Rolling windows.

$dateDiff

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Counts unit boundaries crossed, not simply elapsed milliseconds divided by a constant.

Syntax

{$dateDiff: { startDate, endDate, unit, timezone?, startOfWeek? }}

Use case

Calendar duration calculations.

$dateDiff: practical example
{ $project: { daysOpen: { $dateDiff: { startDate: "$openedAt", endDate: "$$NOW", unit: "day" } } } }
💡 Explanation Counts unit boundaries crossed, not simply elapsed milliseconds divided by a constant. When to use it Calendar duration calculations.
Key Takeaways
  • Counts unit boundaries crossed, not simply elapsed milliseconds divided by a constant.
  • Typical use: Calendar duration calculations.

$dateTrunc

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Truncates dates to a period boundary.

Syntax

{$dateTrunc: { date, unit, binSize?, timezone?, startOfWeek? }}

Use case

Group by day/week/month.

$dateTrunc: practical example
{ $group: { _id: { $dateTrunc: { date: "$createdAt", unit: "week", timezone: "Asia/Kolkata" } }, count: { $sum: 1 } } }
💡 Explanation Truncates dates to a period boundary. When to use it Group by day/week/month.
Key Takeaways
  • Truncates dates to a period boundary.
  • Typical use: Group by day/week/month.

$dateToString

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Formats a BSON Date as a string.

Syntax

{$dateToString: { date, format?, timezone?, onNull? }}

Use case

Report labels.

Important notes

Keep stored dates as BSON Date for filtering/sorting.

$dateToString: practical example
{ $project: { localDate: { $dateToString: { date: "$createdAt", format: "%Y-%m-%d %H:%M", timezone: "Asia/Kolkata" } } } }
💡 Explanation Formats a BSON Date as a string. When to use it Report labels. Caution Keep stored dates as BSON Date for filtering/sorting.
Key Takeaways
  • Formats a BSON Date as a string.
  • Typical use: Report labels.

$dateFromString

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Parses date text into BSON Date.

Syntax

{$dateFromString: { dateString, format?, timezone?, onError?, onNull? }}

Use case

Normalize imported dates.

$dateFromString: practical example
{ $set: { parsed: { $dateFromString: { dateString: "$dateText", format: "%Y-%m-%d", onError: null } } } }
💡 Explanation Parses date text into BSON Date. When to use it Normalize imported dates.
Key Takeaways
  • Parses date text into BSON Date.
  • Typical use: Normalize imported dates.

$dateFromParts / $dateToParts

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Date expressions

What it does

Constructs dates from parts or decomposes dates.

Syntax

Date construction/decomposition expressions

Use case

Build period boundaries or extract multiple components.

$dateFromParts / $dateToParts: practical example
{ $project: { parts: { $dateToParts: { date: "$createdAt", timezone: "Asia/Kolkata" } } } }
💡 Explanation Constructs dates from parts or decomposes dates. When to use it Build period boundaries or extract multiple components.
Key Takeaways
  • Constructs dates from parts or decomposes dates.
  • Typical use: Build period boundaries or extract multiple components.