No need apply regex for your use case.
MongoDb way (v4.2)
We split by https://stackoverflow.com/questions/ or / and take last item.
db.collection.update({},
[
{
$set: {
pictures: {
$map: {
input: "$pictures",
in: {
$arrayElemAt: [
{
$split: [
"$$this",
"https://stackoverflow.com/questions/" // or split by "/"
]
},
-1
]
}
}
}
}
}
],
{
multi: true
})
JS way
We can apply regex, split, etc... and save individually.
db.collection.find({}).forEach(function(doc){
doc.pictures = doc.pictures.map(x => x.replace("https://stackoverflow.com/questions/", ""));
db.collection.save(doc);
})