Here, in my collection, I have "numerical" and "date" field save as string, but I want both of them to be Integer how I can do that. Here is my collection
{
"name": "Thyame",
"salary": "25000",
"dob": "1988-01-25"
}
and the expected output is
{
"name": "Thyame",
"salary": 25000,
"dob": ISODate("1988-01-25T00:00:00.000Z")
}
You may perform MongoDB aggregation to change data type and override your entire collection with $out
operator.
Try this one:
db.collection.aggregate([
{
$project: {
name: "$name",
salary: {
$toInt: "$salary"
},
dob: {
$dateFromString: {
dateString: "$dob",
format: "%Y-%m-%d"
}
}
}
}
//,{$out:"collection"}
])
Note: If you uncomment $out
, it will override all records with aggregation result.