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

99
Visualizações
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 Respostas
Responde à pergunta

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 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