Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Mongo Collection and update a field in every document

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?

like image 738
Mark Pieszak - Trilon.io Avatar asked Nov 04 '15 19:11

Mark Pieszak - Trilon.io


People also ask

Is it possible to update MongoDB field using value of another field?

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.

How do I increment a field in MongoDB?

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.

What does $each do in MongoDB?

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.

How to loop through all MongoDB collections and execute query?

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.

How to update multiple fields of multiple documents in MongoDB?

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:

How to get the collection name of a MongoDB database?

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.


Video Answer


1 Answers

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();
}
like image 188
styvane Avatar answered Oct 21 '22 16:10

styvane