In the schema of mongoDB, I am trying to store current date using date.now(), but it is storing date in UTC format. How can I change the date format in local date?
MongoDB stores dates in BSON Date type, UTC timestamp with 64-bit integer internally. What you want is store dates as local date formatting, just store them already formatted String, not Date. What you want is to see date values as local time, consider using $dateToString aggregation expression.
For example:
db.getCollection('users').aggregate([
{
$project: {
createdAt: { $dateToString: { date: '$createdAt', timezone: 'Asia/Seoul' } }
}
}
])
// { "_id": ..., "createdAt" : ISODate("2020-12-02T04:52:02.237Z") }
// -> { "_id": ..., "createdAt": "2020-12-02T13:52:02.237Z" }