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

806
Vistas
MongoDB renaming a field/key

I want to change a field name ( just the field name but not the value ) in a document in MongoDB. The document looks like this ( below) and it has only one document in the collection of user= Soham :

{_id : ObjectId(xxxxxxxxxxxx),
 user : "Soham",
 age  : 29
}

Now I want to change the field named 'user' to 'name', but not the value of the field. So in the mongo shell I wrote the below commands:

var soh = db.user.find({"user":"Soham"});
soh.name = soh.user;
delete soh.user;
db.user.update({"user":"Roshan"},soh);

When I am running the update command, it's throwing me error. Not sure where I am going wrong as I am new to MongoDB. Any kind of help is appreciated.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There is operator $rename.

db.user.updateMany( {}, { $rename: { "user": "name" } } )
over 4 years ago · Santiago Trujillo Denunciar

0

Use $rename operator.

So, in mongo shell, to update a single document:

db.user.update( { _id: ObjectId(xxxxxxxxxxxx) }, { $rename: { 'user': 'name'} } )

to update all documents

db.user.updateMany( {}, {$rename: { "user": "name" } } )
over 4 years ago · Santiago Trujillo 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