In my attempt to use mongosh in MongoDB, I am trying to replace all instances of selected words in a collection with others.
For example, if I have the following data:
_id:12345678901
name:"Peter Parker"
profession:"Superhero"
_id:12345678902
name:"Peter Parker"
profession:"Journalist"
_id:12345678902
name:"Hulk"
profession:"Software Developer"
I want to replace all occurrences of "Peter Parker" with "Superman" in a collection named "firstList", resulting in:
_id:12345678901
name:"Superman"
profession:"Superhero"
_id:12345678902
name:"Superman"
profession:"Journalist"
_id:12345678902
name:"Hulk"
profession:"Software Developer"
Even though I attempted the following code, it did not work as expected:
db.firstList.replace({
"name": {
"$regex": "/( |^)Peter Parker\b/g",
"$replaceWith": "Superman"
}
})