I have a field in mongo that is an ISODate. Is there an easy way to convert it to unix timestamps?
ie: ISODate("2018-03-30T13:06:05.739-07:00") => 1522440365739
As of 4.0, you can use $toDecimal
. $toInt
would be too small for most timestamps nowadays.
db.collection.aggregate([{
$project: {
date: {'$toDecimal': '$date'}
}
}])
This returns unix timestamps in milliseconds. To convert to seconds, use $divide
by 1000
https://docs.mongodb.com/manual/reference/operator/aggregation/toDecimal/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With