renameCollection Command

Sharding
The renameCollection command cannot be used with sharded collections.

This command can be used to rename an existing collection.

Shell:

> db.oldname.renameCollection("newname")

From drivers without a helper method one can invoke the generic command syntax:

> db.runCommand( { renameCollection: "mydb.oldname", to: "mydb.newname" }

This command is executed atomically and should be safe to use on a production DB. It changes the metadata associated with the given collection and then copies the index metadata from the old namespace to the new one. The amount of time to execute this command is constant and is not related to the size of the collection or the indexes. If there is an open cursor on a collection and it is renamed, the cursor will be invalidated and won't get any more data.

See Also

Follow @mongodb

MongoDB Pittsburgh - May 15
MongoNYC - May 23
MongoDB Paris - Jun 14
MongoDB UK - Jun 20
MongoDC - June 26


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus