How to rename a field from a collection in mongoDB?

How to rename a field from a collection in mongoDB?
By this query you can rename a field from whole collection in mongoDB

db.colleciton.update(
    //  where query like: where active="yes"
    {
        "active" : "yes"
    },
 
    // will remove Name field from current collection
    {
        $rename: {"Name":"Username"}
    },
 
    // options
    {
        "multi" : true  // update all document
    }
);

or you can copy and paste this code above code is with comment that how mongoDB query works

db.colleciton.update(
    {
        "active" : "yes"
    },
    {
        $rename: {"Name":"Username"}
    },
    {
        "multi" : true
    }
);

Comments