$toDate on ObjectId

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

Reference group: Conversion and type inspection

What it does

Extracts timestamp encoded in a standard ObjectId.

Syntax

{$toDate: objectIdExpression}

Use case

Approximate ID generation time.

Important notes

Do not substitute for a business-event timestamp.

$toDate on ObjectId: practical example
{ $project: { approximateCreatedAt: { $toDate: "$_id" } } }
💡 Explanation Extracts timestamp encoded in a standard ObjectId. When to use it Approximate ID generation time. Caution Do not substitute for a business-event timestamp.
Key Takeaways
  • Extracts timestamp encoded in a standard ObjectId.
  • Typical use: Approximate ID generation time.