Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!