Skip to content

Rename a collection in MongoDB

Renaming a collection in MongoDB is quite simple. When you rename a collection, it obtains a lock on the collection till the rename process is complete.

Advertisements

Syntax

db.collection.renameCollection("new_name_for_collection") 

Example

> db.poopcode_users.renameCollection("users")
{ "ok" : 1 }

Here the collection ‘poopcode_users‘ is renamed to ‘users‘.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.