HOWTO · MongoDB
Store, Query, and Sort Dates in MongoDB
Learn how Date() differs from new Date() in MongoDB, then store date values, query date ranges with ISODate(), and troubleshoot date sorting.
On this page
To store a date in MongoDB, pass a JavaScript Date value, usually created with new Date(). In mongosh, Date() without new returns a string; it does not create a date value for a document. This guide shows the distinction, a date-range query with ISODate(), and chronological sorting and troubleshooting.
Use Date() and new Date() in MongoDB
Date() returns the current date and time as a string. Use new Date() when you need a JavaScript Date value that MongoDB can store as BSON Date. For a fixed instant, pass an ISO 8601 timestamp with an explicit UTC marker such as Z:
Run this example:
console.log(new Date("2024-06-01T12:00:00Z").toISOString());
2024-06-01T12:00:00.000Z
This JavaScript example isolates date-value construction; MongoDB operations require a mongosh session. In an insert or update, use the value directly, for example, db.products.insertOne({ item: "apple", dateAdded: new Date() }). The document stores a BSON Date value in dateAdded; it does not store the string returned by Date().
Query and Sort Dates in MongoDB
A date field can be sorted in either direction with sort(): use 1 for ascending order (earlier dates first) and -1 for descending order (later dates first). For example, db.posts.find().sort({ date: 1 }) sorts posts by its date field from earliest to latest, while db.posts.find().sort({ date: -1 }) returns the reverse order. These queries assume the field values are stored consistently as dates.
An ISO-looking string remains a string. If a field contains both strings and BSON Date values, MongoDB sorts according to BSON type order and then value order within each type, so the result may not be chronological across all documents. For reliable date sorting, normalize the field values to BSON Date and use the same field name and sort direction in the query.
If results appear out of order, check that the query targets the intended collection, the sort key matches the stored field name, the values use the BSON Date type consistently, and the direction is 1 or -1 as intended. The collection contains documents; a document contains the date field.
Use ISODate() in MongoDB Date Queries
ISODate() is a mongosh helper used to represent a date value in shell examples; it is not a separate BSON type. Use it to express date bounds in a query. The following example finds documents whose AdmissionDate is from midnight UTC on June 7, 2013 (inclusive) up to midnight UTC on June 8 (exclusive):
db.students.find({ AdmissionDate: { $gte: ISODate("2013-06-07T00:00:00Z"), $lt: ISODate("2013-06-08T00:00:00Z") } })
$gte includes the start instant and $lt excludes the end instant, so adjacent daily ranges do not include the same instant twice. Keep the query field name and the stored BSON Date field consistent.
These mongosh snippets are illustrative and are not presented as runtime output. Run them in a MongoDB shell connected to a database that contains the named collection and fields; find() results depend on that data.
Conclusion
Use new Date() to store a date value, and remember that Date() returns a string. Query date fields with ISODate() bounds in mongosh, and sort consistently typed BSON Date values with sort(). When sorting is unexpected, verify the field name, BSON types, and sort direction.