I have Dates in one Collection that were inserted incorrectly, and are in a simple "2015-09-10"
string format.
I'd like to update them to correct ISO Date format.
I've tried looping through Mongo with forEach()
but I don't know the shell well enough on how to update each document in the collection.
So far I'm at this point:
db.getCollection('schedules').find({}).forEach(function (doc) {
doc.time = new Date( doc.time ).toUTCString();
printjson( doc.time );
// ^ This just prints "Invalid Date"
// Also none of the below work when I try saving them
//doc.save();
//db.getCollection('schedules').save(doc);
});
What's missing here?
Starting from MongoDB 4.2 you can perform Updates with an Aggregation Pipeline. An aggregation pipeline enables more expressive updates including calculated fields and references to other field values in the same document.
In MongoDB, the $inc operator is used to increment the value of a field by a specified amount. The $inc operator adds as a new field when the specified field does not exist, and sets the field to the specified amount. The $inc accepts positive and negative value as an incremental amount.
The $each operator when used with the $addToSet operator, allows us to add multiple elements to an array if they don't exist. When it is used with the $push operator, we can simply append or insert elements in an array.
Loop through all MongoDB collections and execute query? First of all, you need to get your collection with the help of getCollectionNames (). The database name is “test”. Let us loop through all MongoDB collections and execute the query.
In addition, we can also update multiple fields of more than one document in MongoDB. We simply need to include the option multi:true to modify all documents that match the filter query criteria:
First of all, you need to get your collection with the help of getCollectionNames (). The database name is “test”. Let us loop through all MongoDB collections and execute the query.
The best way to do this is using "Bulk" operations
var collection = db.getCollection('schedules');
var bulkOp = collection.initializeOrderedBulkOp();
var count = 0;
collection.find().forEach(function(doc) {
bulkOp.find({ '_id': doc._id }).updateOne({
'$set': { 'time': new Date(doc.time) }
});
count++;
if(count % 100 === 0) {
// Execute per 100 operations and re-init
bulkOp.execute();
bulkOp = collection.initializeOrderedBulkOp();
}
});
// Clean up queues
if(count > 0) {
bulkOp.execute();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With