TransWikia.com

Can i use a custom function with UpdateOne or UpdateMany in MongoDB Shell?

Database Administrators Asked by deadManN on October 3, 2020

I wish to perform update on my database using Update command.
the data structure is as following:

{ 
    "_id" : ObjectId("5d06285570de7d11606275f8"), 
    ...
    ...
    ...
    "ProfilePictures" : [
        {
            "ImageId" : ObjectId("5d0a65d05e82a23764150978"), 
            "CloudinaryUrl" : "<fileAddress>", 
            ...
        }
    ], 
    ...
}

i wish to update my URL, for this first i wrote this function:

var updateHttpToHttps=function(url){ return url.replace(/^http:///i, 'https://'); }

and then i tried to update a new field named CloudinarySecureUrl using data from CloudinaryUrl field, and then i need to fix mismatch in the CloudinaryUrl changing all url starting with https to http:

db.humanResource.user.updateMany({ProfilePictures:{$exists:true}, ProfilePictures:{$ne:null}, ProfilePictures.CloudinaryUrl:{$exists:true}}, {$set:{ProfilePictures.$.CloudinarySecureUrl:updateHttpToHttps(ProfilePictures.$.CloudinaryUrl)}})

formatted version:

db.humanResource.user.updateMany({
    ProfilePictures: {
        $exists: true
    },
    ProfilePictures: {
        $ne: null
    },
    ProfilePictures.CloudinaryUrl: { // I did this because it said for using $ i have to brign a field from that array to my query
        $exists: true
    }
}, {
    $set: {
        ProfilePictures.$.CloudinarySecureUrl: updateHttpToHttps(ProfilePictures.$.CloudinaryUrl)
    }
})

So after writing this update query, i notice, maybe i know that i want to pass the value of ProfilePictures.$.CloudinaryUrl to the javascript function used in this update, but it doesn’t necesserity mean that it will be processed, since all i write is a query language, not javascript code, so the system cannot pass the value to the function…
Is there any way to do this? How?

One Answer

The only way i could find was this, and i ask it in many places, no one answered...:

db.humanResource.user.find({
    ProfilePictures: {
        $exists: true
    },
    ProfilePictures: {
        $ne: null
    }
}).forEach(function (e, i) {
    for(var i=0; i< e.ProfilePictures.length; i++){
         e.ProfilePictures[i].CloudinarySecureUrl = updateHttpToHttps(e.ProfilePictures[i].CloudinaryUrl);
         e.ProfilePictures[i].CloudinaryUrl = updateHttpsToHttp(e.ProfilePictures[i].CloudinaryUrl);
    }
    db.humanResource.user.save(e);
})

Which is based on the answer of this thread:
We can use forEach to decrease load on database and Ram Usage, while updating each record, without loading them all, and then we save that document of collection.

And i could use Print ans JSON stringy as a helping reference before destroying my database:

JSON.stringify is usable in MongoShell with BSON

Print Can be used to print into Mongo Shell

also there was a printjson in the second link, which i didn't notice, at first, i just noticed now.

Answered by deadManN on October 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP