Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

734
Vistas
How to rename mongodb field using regex expression

I'm trying to update the name of a field in the mongodb document using the regex expression on the name of that field but I can't, can someone help me to do it? I have a person document that contains a field with the email of the person, the name of that field is like that "firstname_lastname@gmail.com" and I want to replace "_" character in all email fields by "_dot_". Here is what I did :

    db.getCollection("person").updateMany(
    {
      {'email.'+'.*_.*': { $exists: true }  
    },
    {
      $rename:{'email.'+'.*_.*': 'email.'+'.*_dot_.*'
    });

and here the structure of my document:

    person {
    name: { .... }
    email:{
     firstname_lastname1@gmail.com : {
       .... other fields
     },
     firstname_lastname2@gmail.com : {
       .... other fields
         }
        }
    }

thanks in advance

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As @Wernfried Domscheit suggested in the comment, you should change your schema to avoid using dynamic email address as field name.

Nevertheless, you can use $objectToArray to convert the emails into an array of k-v tuples. Then, $split them by _ and rejoin by _dot_. Finally do a $arrayToObject to convert back to original structure.

db.collection.aggregate([
  {
    "$addFields": {
      "email": {
        "$objectToArray": "$email"
      }
    }
  },
  {
    "$addFields": {
      "email": {
        "$map": {
          "input": "$email",
          "as": "e",
          "in": {
            k: {
              "$ltrim": {
                "input": {
                  "$reduce": {
                    "input": {
                      "$split": [
                        "$$e.k",
                        "_"
                      ]
                    },
                    "initialValue": "",
                    "in": {
                      "$concat": [
                        "$$value",
                        "_dot_",
                        "$$this"
                      ]
                    }
                  }
                },
                "chars": "_dot_"
              }
            },
            v: "$$e.v"
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "email": {
        "$arrayToObject": "$email"
      }
    }
  }
])

Here is the Mongo Playground for your reference.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda