Q:

MongoDB shell: update properties with regex changing existing property

I need to chage this:
userId: 'sdfsd'
pictures: ['https://stackoverflow.com/questions/ask.jpg', 'https://stackoverflow.com/questions/ask2.jpg']

To this:
userId: 'sdfsd'
pictures: ['ask.jpg', 'ask2.jpg']

Is there way to do it with just one command?

A:

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);
})

