Question

I have a mongoDB with dates stored as ms.

when I query the DB with:

> db.drives.find({deviceID:4},{driveDate:1})

I get

{ "_id" : ObjectId("52725be3a3d27f8c9eee4022"), "driveDate" : 1383226033496 }

I want to display this date within the result in the mongoshell in a readable format. Is there a way to 'convert the date on the fly', so that the result will look like this?:

{ "_id" : ObjectId("52725be3a3d27f8c9eee4022"), "driveDate" : 'Thu Oct 31 2013 07:27:13 GMT-0600 (CST)' }

Thanks.

Was it helpful?

Solution

In MongoDB shell you can use this:

db.drives.find({deviceID:4},{driveDate:1}).forEach(function (doc) {
  doc["driveDate"] = new Date(doc["driveDate"])
  printjson(doc)
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top