Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NumberLong to Date in MongoDB's shell?

I am storing unix timestamp as MongoDB's NumberLong type (millisecond) , how to convert to human readable date string in the Mongo shell?

like image 233
Ryan Avatar asked Oct 02 '22 02:10

Ryan


2 Answers

NumberLong(123123123123131).toNumber().toString()
like image 168
xiao_chong Avatar answered Oct 13 '22 10:10

xiao_chong


For my own future reference (and incorporating the other answers):

db.mycollection.aggregate([
  {$match:{"userId":"d1792d83-d854-44d5-9jc5-e3e20435958b"}},
  {$project:{newDate:Date("$numberLongDate")}}
])
like image 26
fred271828 Avatar answered Oct 13 '22 12:10

fred271828