Date (DateTime)

Last updated: 2026-10-09

Date (DateTime)

Date is MongoDB's date-time type, stored internally as a 64-bit millisecond count since the Unix epoch. It is constructed from ISO date strings (new Date("2026-09-25")) or millisecond timestamps. Dates are range-filtered in queries with $gt/$lt and friends.

CategoryBSON Types
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

new Date("<ISO string>")
new Date(<ms timestamp>)
ISODate("<ISO string>")

▶ Example

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

// Date: orders created today or later
db.orders.find({
  createdAt: { $gte: new Date("2026-09-25T00:00:00Z") }
})

Output:

// Returns documents with createdAt >= 2026-09-25 00:00 UTC

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between Date and Timestamp?
ADate stores a millisecond time point; Timestamp is for internal oplog (with an increment counter) and is rarely used by applications.
QHow do I store a millisecond timestamp?
AStore new Date(ms) as a Date — don't save the raw number in a numeric field.
QCan I query dates with strings?
AAvoid it — strings and Dates are different types; $gt/$lt comparisons silently miss. Store Dates, then query them.
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%

🙏 帮我们做得更好

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

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