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

174
Views
how to add a number to a integer field using nodejs and mongodb

I have a collection like this.

 {
"_id": "6137392141bbb7723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":9000000001
},
{
"_id": "6137392141bbe30723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":9000000002`
}

Aim is to update the phonenumber field with 91. i want to insert 91 infront each and every phonenumber of users.

{
"_id": "6137392141bbb7723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":919000000001
},
{
"_id": "6137392141bbe30723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":919000000002
}

im trying with updateMany

const doc = await User.updateMany(
            {}, 
            { $set: { 
                //missing here
              }},
            {
                new: true, 
                runValidators : true
            });```
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'm not sure that it's the best solution, but

parseInt('91' + user.phonenumber.toString())
about 4 years ago · Juan Pablo Isaza Report

0

EITHER add 910000000000

data.forEach(item => item.phonenumber += 910000000000)

OR

concatenate "91" and convert the string back to number

data.forEach(item => item.phonenumber = +("91"+item.phonenumber))

const data =  [{
"_id": "6137392141bbb7723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":9000000001
},
{
"_id": "6137392141bbe30723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":9000000002
}]

data.forEach(item => item.phonenumber += 910000000000)
console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

Let Suppose that you have a data from as an array

[{
"_id": "6137392141bbb7723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":919000000001
},
{
"_id": "6137392141bbe30723",
"email": "brooke@cagle.com",
"lastname": "Cagle",
"firstname": "Brooke",
"phonenumber":919000000002
}]

First of all, you need to parse it to JavaScript, if it's a file you require fs module to be able to extract the data.

// parse to js
const array = JSON.parse(data);
const formattedArray = array.map( user => ({...user, phonenumber:  Number(`91${user. phonenumber}`) })) 
const newData = JSON.stringify(formattedArray)

That is it, I hope it can help you

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!