HOWTO · MongoDB
How to Convert Timestamp Into Date in MongoDB
This tutorial educates about how to convert timestamp into date in MongoDB.
On this page
A timestamp can represent different BSON values, so choose the conversion from the field’s stored type. Numeric epoch timestamps and strings containing epoch milliseconds use different expressions from BSON Timestamp values. After converting values to BSON Dates, you can group documents by a formatted calendar-day key and count them.
Convert a Timestamp to a Date in MongoDB
First inspect a representative document’s field type. In the legacy mongo shell, typeof reports the JavaScript value category; BSON Timestamp values can appear as JavaScript objects, so this check alone does not identify their exact BSON type. Use the aggregation $type expression when the BSON distinction matters: for example, {$project: {valueType: {$type: "$datetime"}}} reports the stored BSON type for each document.
These MongoDB shell expressions are examples, not runtime-verified output in this article. Replace collection_name and datetime with your collection and field names. The conversion below assumes the numeric values represent milliseconds since the Unix epoch; seconds-based values need a deliberate unit conversion before use.
For an epoch-millisecond value, JavaScript’s Date also uses milliseconds. Run this example:
console.log(new Date(1655448286502).toISOString());
2022-06-17T06:44:46.502Z
Convert Numeric Millisecond Timestamps
For a numeric field containing Unix epoch milliseconds, MongoDB’s $toDate converts the number to a BSON Date. This compact shell example shows the expression; adapt it to your collection:
db.collection1.aggregate([{$project: {convertedDate: {$toDate: "$datetime"}}}])
For example, a value such as 1655448286502 represents 2022-06-17T06:44:46.502Z. Do not apply this expression to a number measured in seconds as though it were milliseconds: the resulting date would be far from the intended time. If the source unit is seconds, multiply by 1000 in the aggregation before $toDate.
Convert String Millisecond Timestamps
A string such as "1655445247168" is not itself a date or a numeric BSON value. Convert the numeric string to a 64-bit integer with $toLong, then pass that result to $toDate:
db.collection2.aggregate([{$project: {convertedDate: {$toDate: {$toLong: "$datetime"}}}}])
This pattern assumes each string contains a base-10 integer in epoch milliseconds. Strings in another date format need a parsing approach appropriate to that format; they should not be treated as millisecond counts. MongoDB conversion errors can occur when a value cannot be converted, so check that the field’s stored values match the assumption before applying the expression to a collection.
Convert BSON Timestamp Values
BSON Timestamp is a distinct BSON type, commonly used for internal MongoDB operations. It is not an ordinary epoch-millisecond number: it stores a time value in seconds plus an ordinal. MongoDB’s $toDate documentation lists BSON Timestamp as a supported input and says the result is the date corresponding to that timestamp. The conversion therefore uses its seconds-based time value, not the ordinal as milliseconds or fractional seconds. This differs from the numeric case, where $toDate interprets the number as milliseconds. See also MongoDB’s BSON Timestamp documentation for the type’s seconds and ordinal components.
For an actual BSON Timestamp field, convert it directly:
db.collection3.aggregate([{$project: {convertedDate: {$toDate: "$datetime"}}}])
The returned Date represents the timestamp’s whole-second time. It does not preserve the Timestamp ordinal as date precision, and the conversion does not make a BSON Timestamp interchangeable with an application event time. Confirm that the stored field is the Timestamp type and that its seconds component is the time you intend to report.
Group Converted Dates and Count Documents by Day
To count documents by day, first project a converted Date and then group on a $dateToString result. The format value %Y-%m-%d produces a year-month-day key. The timezone option below explicitly uses UTC, so the grouping follows UTC calendar days rather than the database server’s local timezone:
db.collection1.aggregate([{$project: {convertedDate: {$toDate: "$datetime"}}}, {$group: {_id: {$dateToString: {format: "%Y-%m-%d", date: "$convertedDate", timezone: "UTC"}}, count: {$sum: 1}}}, {$sort: {_id: 1}}])
Use the corresponding conversion expression for string or BSON Timestamp fields before grouping. Grouping by a formatted string intentionally combines all instants that share that date in the selected timezone; it does not produce a BSON Date as the group key. If you need a different calendar day boundary, choose its timezone explicitly and apply it consistently. $sum: 1 adds one for each document in a group, while sorting by _id makes the resulting date keys easier to scan.