I need to convert and update date fields in 12 hrs format in one Collection(demo) through Mongo Db script.
Sample data is given below;
[
{
"_id": "dgdjh13233",
"a": [
{
"b": [
{
"date": "03/26/2022 23.03 pm"
},
{
"date": "03/26/2022 04.03 pm"
}
]
}
]
},
{
"_id": "dgdjh132365",
"a": [
{
"b": [
{
"date": "03/26/2022 22.03 pm"
},
{
"date": "03/28/2022 08.03 pm"
}
]
}
]
}
]
I need to update date fields for ex "date":"03/26/2022 22.03 pm" to "03/26/2022 10.03 pm". Is there any way to do this throgh scripts or can we find which all the data which are in 24 hrs format so that we can change it manually?
I am trying to fetch data throug scripts which are in 24 hours format but unable to do due to inadeqaute knowledge of mongodb.
db.getCollection("demo").find({},
{
"a.b.date":"$a.b.date",
"_id":NumberInt(0)
}
);
"03/26/2022 23.03 pm" is a string, so if you want to convert from 12 to 24 hour in the mongod server, you will first need to convert that to a datetime with $dateFromString and then convert it back to a string in the format you want with $dateToString