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.