Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda