When changes the collection name If you may received an error, errmsg Exception : you can not rename a Shared Collection,

The below steps into the MongoDB to change collection name,



mongos> show dbs;
admin      0.016GB
testdb     0.063GB

mongos> use testdb
switched to db testdb

mongos> show collections
mydata
testcol
system.indexes
thelinux
thelinuxfaq


mongos> db.testcol.renameCollection( "testcol123" )
{
    "code" : 13138,
    "ok" : 0,
    "errmsg" : "exception: You can't rename a sharded collection"
}
mongos> 


solution :


On a Shsared collections can not supported the rename existing collection on MongoDB.

You can create and copy all the docs to a new collection in same database or create multiple collections based on a weekly or date wise, and use as the current one.


To know few information renameCollection() :

db.collection.renameCollection() is not supported on sharded collections.

db.collection.renameCollection() blocks all database activity for the duration of the operation.

db.collection.renameCollection() fails, if target is the name of an existing collection and you do not specify dropTarget: true.