AnswerBun.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!

Related Questions

HAPROXY for Postgres Failover Master/Replica

0  Asked on December 31, 2020 by jayson-gonzaga

       

Error 0xc000007b with Oracle 19c

2  Asked on December 28, 2020 by ben-a

       

Join to tables dynamically depending on data

2  Asked on December 28, 2020 by yellephen

   

PostgreSQL exits the shell when importing large data

2  Asked on December 26, 2020 by 3bdalla

   

Spike and time out errors every x th minute

1  Asked on December 25, 2020 by beginnerdba

     

Oracle RAC and DataGuard configuration

1  Asked on December 25, 2020 by yifan

         

Segragating First LSN and Last LSN from Output MySQL

0  Asked on December 24, 2020 by jabir-baig

     

Lossless Join Decomposition Criteria

1  Asked on December 23, 2020 by kevin-wu

   

Mongodb Upgrade from 1.8.2 to 4.4

0  Asked on December 23, 2020 by amanullah

   

SQLite database becomes corrupt

1  Asked on December 22, 2020 by jkozma

 

How do I export a multi table database with exceptions?

2  Asked on December 21, 2020 by soberuprising

     

MS SQL login periodically doesn’t work

2  Asked on December 20, 2020 by alonzo-hess

   

PostgreSQL: Find row with JSON object attribute in array

1  Asked on December 20, 2020 by karlsc

     

Hotel booking schema

1  Asked on December 20, 2020 by rakoun

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir