How to substitute part of the string in MongoDB with an update query.
For example, I have this:
_id: 123
url:"test.com?from_user=1"
name:"Superhero"
_id:124
url:"regex.com?from_user=3"
name:"Journalist"
_id:123
url:"test.com?from_user=2"
name:"Software Developer"
And I want to substitute all documents with _id:123
part of url
= "test.com" to "sample.com" to get this:
_id: 123
url:"sample.com?from_user=1"
name:"Superhero"
_id:124
url:"regex.com?from_user=3"
name:"Journalist"
_id:123
url:"sample.com?from_user=2"
name:"Software Developer"
I tried this way
$collection->updateMany([
'_id' => 123,
'url' => [
'$regex' => "test.com"
]
], [
'$set' => [
"url" => [
'$replaceAll' => [
"input" => '$url',
"find" => "test.com",
"replacement" => "example.com"
]
]
]
], [
'multi' => true
])
But here it displays an error:
The dollar ($) prefixed field '$replaceAll' in 'url.$replaceAll' is not valid for storage.