MONGOSH updateOne gives error "could not be cloned"

This is a sample of my document structure:

[
    {
      "_id": "123",
      "Config": "Some text and then Old substring config for 123"
    },
    {
      "_id": "456",
      "Config": "Some more text for another config Old substring config for 456"
    }
]

Expected outcome after update:

[
    {
      "_id": "123",
      "Config": "Some text and then New substring config for 123"
    },
    {
      "_id": "456",
      "Config": "Some more text for another config New substring config for 456"
    }
]

I’m trying to replace a substring in the Config property for the whole collection using below code.

db.MyConfigs.find().forEach(function(doc){ 
    var newConfig = doc.Config.replace('Old substring', 'New substring');   
    db.MyConfigs.updateOne(
        {"_id", doc._id},
        {$set: { "Config": newConfig } }
    );    
});

I’m getting below error when I execute above code in MONGOSH.

Error: clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in r?r.column:...<omitted>...)} could not be cloned.
    at Object.serialize (node:v8:332:7)
    at u (<Path>\AppData\Local\MongoDBCompass\app-1.34.1\resources\app.asar.unpacked\node_modules\@mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:583764)
    at postMessage (<Path>\AppData\Local\MongoDBCompass\app-1.34.1\resources\app.asar.unpacked\node_modules\@mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:584372)
    at i (<Path>\AppData\Local\MongoDBCompass\app-1.34.1\resources\app.asar.unpacked\node_modules\@mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:5

MondoDB Version: 3.6.8

Can anyone help with the issue here?

Thanks.

Hi @Stak and welcome in the MongoDB Community :muscle: !

I’ll start with a version compatibility issue: https://www.mongodb.com/docs/mongodb-shell/connect/#supported-mongodb-versions.

Looks like Mongosh is for MDB 4.0 and above. Can you try with the old “mongo shell” and see if you have the same problem?

That being said, for this use case, I would use a bulk write operation to send the updateOne operations in a single bulk instead of firing them one by one. If you really have a lot, I would send them 1000 by 1000 for a first try.

Cheers,
Maxime.

I just tested your script and it’s working fine for me on MongoDB 6.0.3 and Mongosh 1.6.0 except that I just had to correct the typo in the filter (":" instead of “,”).

db.MyConfigs.find().forEach(function(doc){
    var newConfig = doc.Config.replace('Old substring', 'New substring');   
    db.MyConfigs.updateOne(
        {"_id": doc._id},
        {$set: { "Config": newConfig } }
    );    
});
2 Likes

Thanks for the response. Regarding the typo, yes it’s just a typo in the question, query was executed with proper syntax.

I guess it has to do with MongoDB version. Since I don’t have access to a later version, i’ll try this again in a later date.

Once again, thanks for taking the time to answer and verify that it does work in later version.

1 Like