Nafeez
Nafeez

Reputation: 48

timestamp to YYYYMMDD convertion in aggregation in mongodb

throws error message assert: command failed: { "ok" : 0, "errmsg" : "$dateToString is only defined on year 0-9999, tried to use year 292278994", "code" : 18537 } : aggregate failed

for timestamp "1481045214411"

db.collection.aggregate( [ { $group:{ _id : "$department", startTimeX : { $min:{$dateToString: {format:"%Y%m%d",date: new Date("$date")}} } } } ] )

Upvotes: 0

Views: 610

Answers (1)

felix
felix

Reputation: 9285

You can't create a Date from milisecond timestamp directly, but a simple trick can help you achieve this :

db.collection.aggregate([
   {
      $group:{
         _id:"$department",
         startTimeX:{
            $min:{
               $dateToString:{
                  format:"%Y%m%d",
                  date:{
                     $add:[
                        new Date(0),
                        "$date"
                     ]
                  }
               }
            }
         }
      }
   }
])

Upvotes: 1

Related Questions