How can I rename a collection in MongoDB?

后端 未结 5 756
醉梦人生
醉梦人生 2020-12-24 00:36

Is there a dead easy way to rename a collection in mongo? Something like:

db.originalCollectionName.rename(\'newCollectionName\');

And if

5条回答
  •  心在旅途
    2020-12-24 01:09

    You can use the following syntax to rename an existing collection in MongoDB.

    db.originalCollectionName.renameCollection('newCollectionName')

    For instance, if your existing collection name is 'demo' and want to rename to 'demo_updated' then, the query would be as follows:-

    db.demo.renameCollection('demo_updated')
    

    Thanks!

提交回复
热议问题