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

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

0

There is operator $rename.

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

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