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.
There is operator $rename.