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

204
Visualizações
how to add number to field in every document in mongodb using nodejs

My collection has a phonenumber field. I want to add 91 in front of phonenumber.

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

I want to update my collection like this.

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

I'm trying with this code but it's not updating the field.

var x = await User.find({});
        x.forEach(async function(d)
            { 
                var phone = d.phonenumber;  
                var newPhoneNumber = 910000000000+phone;
                console.log(newPhoneNumber);

                //update 
            await User.updateMany({},{$set:{phonenumber:newPhoneNumber}},
                {
                    new: true, 
                    runValidators : true
                });
            })

I'm getting only one value for all documents like this:

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

I don't know whether this approach is correct or not.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Playground

db.collection.update({},
//To match all documents
[{
  $set: {
    key: {
      $toInt: {
        $concat: [
          "91",
          {
            $toString: "$key"
          }
        ]
      }
    }
  }
}
],
//Concatenate 91 and the value in the field
{multi: true
})

This is applicable for mongo versions from 4.2. This is aggregation pipeline which is used in update query.

Reference

EDIT:

You would have got overFlow error. You could use toLong in this case.

db.mybooks.update({},
//To match all documents
[{
  $set: {
    key: {
      $toLong: {
        $concat: [
          "91",
          {
            $toString: "$key"
          }
        ]
      }
    }
  }
}
],
//Concatenate 91 and the value in the field
{multi: true
})

It's tested piece of code.

about 4 years ago · Juan Pablo Isaza Relatório

0

I have found a solution. Maybe, this works,

let mobile = 9876543210;
let phone = `91${mobile}`;
let final = Number(phone)
console.log(typeof(final), final+3);

So,

var phone = d.phonenumber;
var newPhoneNumber = Number(`91${phone}`);
console.log(newPhoneNumber);

Read more about Number() : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

about 4 years ago · Juan Pablo Isaza 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