If you ever inherit a MongoDB collection where date fields were stored as strings instead of proper Date types, sorting and range queries get awkward fast. Lexicographic comparisons mostly work for ISO-8601, but anything else falls apart, and indexes can't help the way they would on real BSON dates.
The full post has the one-liner I keep reaching for: an updateMany with a $type: "string" filter and a $toDate aggregation pipeline that converts the field in place. Run it once, and the rest of your queries get to behave like MongoDB intended.
Originally published at andreasbergstrom.dev — read the full post there.
Top comments (0)