It's possible to changes the name of an existing collection in MongoDB database. Assume that the database name is "testdb" and collection name is "testcollection" there are two ways to changing collection name,
Command Syntax :
{ renameCollection: "<source_namespace>", to: "<target_namespace>",
dropTarget: <true|false> }
dropTarget: <true|false> }
Enter into mongo database and choose database,
# mongo
> use admin
> db.runCommand( { renameCollection: "testdb.testcol", to: "testdb.testcol101" } )
The above command is renameCollection testcol to testcol101.
Another way :
The mongo shell provides the db.collection.renameCollection(), this command help us to rename collections within the same database.
> use testdb
> db.testcol.renameCollection( "testcol101" )
Comments (0)