Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
Replace all the same words in mongoDB using REGEX

Im trying, in mongoDB using mongosh,to replace all the words that i select in a collection for others.

Example, I have this:

_id:12345678901
name:"Peter Parker"
profession:"Superhero"

_id:12345678902
name:"Peter Parker"
profession:"Journalist"

_id:12345678902
name:"Hulk"
profession:"Software Developer"

And I want to replace all the "Peter Parker" with "Superman" in a collection called "firstList" to get this:

_id:12345678901
name:"Superman"
profession:"Superhero"

_id:12345678902
name:"Superman"
profession:"Journalist"

_id:12345678902
name:"Hulk"
profession:"Software Developer"

Im trying this, but it doesnt work:

db.firstList.replace({
  "name": {
    "$regex": "(/( |^)Peter Parker\b/g, "$Superman")"
  }
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to your data you don't need regex, you can use the word into find stage as it is.

You can use an aggregation query into update using [] like this:

db.collection.update({
  "name": {
    "$regex": "Peter Parker"
  }
},
[
  {
    "$set": {
      "name": {
        "$replaceAll": {
          "input": "$name",
          "find": "Peter Parker",
          "replacement": "Superman"
        }
      }
    }
  }
],
{
  "multi": true
})

Example here

By the way, you can use the regex into find object if you want. But if you want to match the entire word "Peter Parker" you can use without regex.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda