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

96
Views
how to convert 10 digit number field to string field in mongodb

i want to update mobile field to string in mongodb.

{
"_id": "1373b7723",
"firstname": "name1",
"mobile":1000000099
},
{
"_id": "137be30723",
"firstname": "name2",
"mobile":1000000088
}

i need an output like this.

{
"_id": "1373b7723",
"firstname": "name1",
"mobile":"1000000099"
},
{
"_id": "137be30723",
"firstname": "name2",
"mobile":"1000000088"
}
db.users.updateMany(
    {}, //To match all documents
    [{ $set: {mobile: { $concat: [ "", "$mobile" ] } } }], 
    {
        new: true, 
        runValidators : true
    });

i tried with the above code but its not getting the desired output.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I did something similar earlier, but it was not conversion to string but to Date. See this

You can also use $toString as mentioned here. See this example similar to your use case

Try below if it works for you.

db.users.find({}).forEach(function (doc) {
  db.users.updateOne(
    {
      _id: doc._id,
    },
    {
      $set: {
        mobile: String(doc.mobile)
      },
    }
  )
})

or modified version of your code

db.users.updateMany(
    {}, //To match all documents
    [{ $set: {mobile: { $toString: {$toLong:"$mobile"}  } } }], 
    {
        new: true, 
        runValidators : true
    });
about 4 years ago · Juan Pablo Isaza 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!