How can I rename a collection in MongoDB?

后端 未结 5 746
醉梦人生
醉梦人生 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

    For those who cannot rename, because the name causes an issue like: SyntaxError: Unexpected token ILLEGAL, it is because the name is illegal.

    You can work around this by calling with brackets notation: db["oldCollectionILLEGALName"].renameCollection("someBetterName")

提交回复
热议问题