I'm using MongoDB and Robo 3T. In some documents I have a field parent
with the value "0"
(string). I want to replace it with "NA"
. In order to get all records I did:
db.getCollection('mycollection').find({}).forEach(function(doc) {
var json = JSON.stringify( doc );
if ( json.match(/"parent":"0"/) != null ) {
printjson(doc);
}
});
Now, how can I replace the "parent":"0"
with "parent":"NA"
in all records?
EDIT: Another attempt:
db.getCollection('mycollection').find({}).forEach(function(doc) {
var json = JSON.stringify( doc );
if ( json.match(/"parent":"0"/) != null ) {
var obj = JSON.parse(json, (key, value) => {
return (key === 'parent' && value === '0')
? value.replace('0', 'NA')
: value;
});
}
//doc = obj;
printjson(obj);
});
Which works. But updating the record by doing doc = obj
doesn't work. How to fix it?