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.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (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
- Stored as UTC milliseconds; displayed in the client's timezone
- Date vs string: "2026-09-25" is a string; new Date("2026-09-25") is a Date
- Range queries use $gte/$lt (half-open intervals)
- Format output with $dateToString in aggregation
🏷️ Related Tags
💡 Related Cases
Deleting Documents and Getting Started with Node.js
Documents and BSON: The Cornerstone of MongoDB Data
Inserting Documents: A Detailed Explanation of `insertOne` and `insertMany`
Mongoose Models and Schema Design
📚 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.