$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.