$currentDate (Current Date)

Last updated: 2026-10-09

$currentDate (Current Date)

$currentDate sets a field to the current time as a Date or Timestamp. `{ $currentDate: { lastModified: true } }` stores a Date by default; `{ $currentDate: { lastModified: { $type: 'timestamp' } } }` stores a Timestamp. More precise than client-side new Date() and avoids server/client clock skew.

CategoryUpdate Field
MongoDB Version3.6+
Example Languagemongosh (Shell)

📝 Syntax

{ $currentDate: { <field>: true | { $type: 'timestamp' | 'date' } } }

⚙️ Parameters

fieldThe field to set to the current time.
typetrue stores a Date; 'timestamp' or 'date' can be explicit.

Returns:The update result

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $currentDate: record modification time
// true stores a Date by default
db.articles.updateOne(
  { _id: 1 },
  { $currentDate: { lastModified: true } }
)

// Explicit Timestamp
db.articles.updateOne(
  { _id: 1 },
  { $currentDate: { lastModified: { $type: 'timestamp' } } }
)

Output:

// lastModified is set to the server time as Date or Timestamp

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between $currentDate and $set: new Date()?
A$currentDate reads the server clock consistently; $set uses client time, which can differ across clients.
QCan it store a Timestamp?
AYes, specify { $type: 'timestamp' }.
QDoes $currentDate overwrite existing values?
AYes, it overwrites with the current time.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏