I want to update the subject field that starts with http://test and consists of /base in the value with the value without base
for example "http://test/timespan/base/1", as it starts with https://test and has base in it. Then i want to update it to http://test/timespan/1
so at last the subject array becomes :
"subject": [ "http://test/concept/114", "http://test/timespan/1", "http://wikidata/1233" ]
Document sample
{
"_id": {
"$oid": "1233"
},
"type": "Entity",
"subject": [
"http://test/concept/114",
"http://test/timespan/base/1",
"http://wikidata/1233"
]
}
CodePudding user response:
You can use a conditional update, like so:
db.collection.updateMany({},
[
{
$set: {
subject: {
$map: {
input: {
$ifNull: [
"$subject",
[]
]
},
in: {
$cond: [
{
$regexMatch: {
input: "$$this",
regex: "http:\\/\\/test.*base"
}
},
{
$replaceAll: {
input: "$$this",
find: "base/",
replacement: ""
}
},
"$$this"
]
}
}
}
}
}
])