Convert string dates to Date-type in MongoDB
Sorting and range queries against date fields silently break when the values are stored as strings. Convert them in place — the pipeline form of updateMany (MongoDB 4.2+) handles it in one pass, and the $type: "string" filter keeps re-runs idempotent:
1db.collection.updateMany(
2 { created: { $type: "string" } },
3 [{ $set: { created: { $convert: { input: "$created", to: "date", onError: "$created", onNull: "$created" } } } }]
4)$convert parses ISO 8601 strings as UTC, so ambiguous formats like "2022-11-05" become midnight UTC and may shift on display in non-UTC zones. onError: "$created" and onNull: "$created" leave malformed or missing values untouched so one bad document doesn't abort the batch.