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

131
Vistas
Change capital letters in mongo to camel casing?

I have a collection named User, which contains the the fields firstName and secondName. But the data is in capital letters.

{
  firstName: 'FIDO',
  secondName: 'JOHN',
  ...
}

I wanted to know whether it is possible to make the field to camel case.

{
  firstName: 'Fido',
  secondName: 'John',
  ...
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use a helper function to get your desired answer.

function titleCase(str) {
    return str.toLowerCase().split(' ').map(function(word) {
        return word.replace(word[0], word[0].toUpperCase());
    }).join(' ');
}

db.User.find().forEach(function(doc){
    db.User.update(
        { "_id": doc._id },
        { "$set": { "firstName": titleCase(doc.firstName) } }
    );
});
over 4 years ago · Santiago Trujillo Denunciar

0

Run an update operation with aggregate pipeline as follows:

const titleCase = key => ({
   $concat: [
      { $toUpper: { $substrCP: [`$${key}`,0,1] } },
      { $toLower: {
            $substrCP: [
               `$${key}`, 
               1,
               { $subtract: [ { $strLenCP: `$${key}` }, 1 ] }
            ]
      } }
   ]
});
db.User.updateMany(
   {},
   [
      { $set: { 
         firstName: titleCase('firstName'),
         secondName: titleCase('secondName')
      } }
   ]
)

Mongo Playground

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