-1

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
            });```
sai krishna
  • 131
  • 5
  • Hello, first I want to ask about the data. Is it from MongoDB or just a JSON file ? – Anas Ben Yaiche Jan 17 '22 at 10:12
  • Does this answer your question? [Modify object property in an array of objects](https://stackoverflow.com/questions/16691833/modify-object-property-in-an-array-of-objects) – pilchard Jan 17 '22 at 10:13
  • You ask `how to add a number to a integer field using nodejs` but your attempt is not node but mongo – mplungjan Jan 17 '22 at 10:21
  • 1
    You might not want to use a number type for phone number fields - I would probably prefer a string type. See https://en.wikipedia.org/wiki/E.164 – soupy-norman Jan 17 '22 at 10:24

3 Answers3

0

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

parseInt('91' + user.phonenumber.toString())
utf_8
  • 105
  • 11
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)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
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

Anas Ben Yaiche
  • 225
  • 3
  • 7