Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a document field in a MongoDB? [duplicate]

Tags:

mongodb

How is possible to rename a field in multiples documents in a MongoDB? I have a collections with elements like this, and I want the rename the field "name" by "userName" in all the collection

{
    "name"       : "luisPerez",
    "address"    : "Gaiman",
    "addressFmt" : "Gaiman, Chubut Province, Argentina",
    "loc" : {
        "type" : "Point",
        "coordinates" : [ -65.4920111, -43.2895976  ]
    }, 
}
like image 978
Pablo Ezequiel Inchausti Avatar asked Mar 11 '17 14:03

Pablo Ezequiel Inchausti


People also ask

How do I update multiple fields in MongoDB?

Update Multiple Fields of a Single Document. We can use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value.


2 Answers

You may use rename operator to rename your fields

db.coll.update({}, {$rename:{"name":"userName"}}, false, true);

false : upsert:false
true : multi:true 
like image 153
Rahul Avatar answered Sep 22 '22 21:09

Rahul


Posible duplicity with:

How can I rename a field for all documents in MongoDB?

Posible use $rename

db.collection({}, {$rename:{"name":"userName"}}, false, true);
like image 44
Matej Marconak Avatar answered Sep 25 '22 21:09

Matej Marconak