1

In the collection, time stamp field is stored as a string.

{
    "_id" : ObjectId("59a780a3da2100974a36cdca"),
    "datetime" : "2016-08-17 0:59"
    ...
}

I am tring to convert the string to date in the first stage of Mongo aggregation piple line.

db.collection.aggregate([
    {
        $addFields: {
            ts: new Date('$datetime')
        }
    }
])

I have done this outside the pipeline, which works fine.

{
    "_id" : ObjectId("59a780a3da2100974a36cdca"),
    "datetime" : "2016-08-17 0:59",
    "ts" : ISODate("2016-08-17T00:59:00.000-04:00"),
}

However, if I do this inside aggregation. I got the following

{
    "_id" : ObjectId("59a780a3da2100974a36cdca"),
    "datetime" : "2016-08-17 0:59",
    "ts" : ISODate("1969-12-31T19:00:00.000-05:00")
}
  • Possible duplicate of [Converting string to date in mongodb](https://stackoverflow.com/questions/10942931/converting-string-to-date-in-mongodb) – dnickless Dec 04 '17 at 22:33
  • Possible duplicate of [How can convert string to date with mongo aggregation?](https://stackoverflow.com/questions/34837489/how-can-convert-string-to-date-with-mongo-aggregation) – thegreenogre Dec 04 '17 at 22:53

0 Answers0