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

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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