Home > database >  MongoDB update by regex before last slash
MongoDB update by regex before last slash

Time:03-24

I need help with mongodb query. I have documents containing field url. For example:

{
  _id: 123,
  url: https://some-url.com/category/v1/12345.jpg
}

I have to update url in each doc. I have to replace the url, but the image part. It should like this:

https://anothe-url.com/some-new-cat/v2/12345.jpg

How can I achieve this?

CodePudding user response:

Regular expressions are usually slow. I would suggest this:

url = "https://some-url.com/category/v1/12345.jpg"
"https://anothe-url.com/some-new-cat/v2/"   url.split("/").pop()
  • Related