The string representing the date is currently invalid, however a simple adjustment can make it valid. All that needs to be done is to insert a "T"
between the "date" and "time" components, after which new Date()
will function correctly.
db.gmastats.find({ "date": { "$type": 2 } }).forEach(function(doc) {
doc.date = new Date(doc.date.split(" ").join("T"));
db.gmastats.save(doc);
})
It's crucial to note that simply searching for data that is not in BSON Date format is risky. It's imperative to focus exclusively on "string" data when working with strings.
Optimally, these actions should be performed using "Bulk" operations:
var ops = [];
db.gmastats.find({ "date": { "$type": 2 } }).forEach(function(doc) {
ops.push({ "updateOne": {
"filter": { "_id": doc._id },
"update": { "$set": { "date": new Date(doc.date.split(" ").join("T")) } }
}});
if ( ops.length == 1000 ) {
db.gmastats.bulk_write(ops);
ops = []
}
})
if ( ops.length > 0 ) {
db.gmastats.bulk_write(ops);
}
This approach will significantly enhance speed and ensure exclusive updates solely to the "date"
property, without interfering with concurrent write processes.