Reputation: 1327
I want to format the date time into an specific format on the mongo shell output
My query
db.getCollection('people').find({
date: {
$gte: ISODate("2017-04-24T14:04:34.447Z")
}
},
{
_id: 0,
age: 0,
}
);
My output against this query:
/* 1 */
{
"user_id" : "bcd020",
"status" : "D",
"date" : ISODate("2017-04-24T14:04:34.447Z")
}
/* 2 */
{
"user_id" : "bcd021",
"status" : "D",
"date" : ISODate("2017-04-24T14:04:34.447Z")
}
What i want is to format the datetime in the output something like,
/* 1 */
{
"user_id" : "bcd020",
"status" : "D",
"date" : 2017-04-24 14:04:34
}
/* 2 */
{
"user_id" : "bcd021",
"status" : "D",
"date" : 2017-04-24 14:04:34
}
Upvotes: 12
Views: 22088
Reputation: 1327
Solution is using aggregation pipeline as stated by Veeram in comments section
db.getCollection('people').aggregate([
{
$project:{
datetime: {$dateToString: {format: "%G-%m-%d %H:%M:%S",date: "$datetime"}},
age : 1
}
}
]);
Upvotes: 11