72

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

db.originalCollectionName.rename('newCollectionName');

And if not, what is the best way to go about effectively renaming one?

Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103

6 Answers6

118

Close. Use db.originalCollectionName.renameCollection('newCollectionName')

See http://www.mongodb.org/display/DOCS/renameCollection+Command

nav
  • 3,035
  • 1
  • 20
  • 24
  • 4
    hint: try using tab complete to find things you think might be there ;) – nav Jan 04 '12 at 20:02
  • 1
    +1 I also found that this will error if the collection that you wish to rename to (the 'target collection name') already exists. IF you are happy for said collection to be **dropped**; then you can pass 'true' as the second argument, e.g. `db.originalCollName.renameCollection('alreadyExistingCollName', true)` – AlexP Feb 24 '15 at 23:15
  • after the rename, will the indexes and everything be kept? – user2810081 Dec 03 '16 at 00:30
  • How can I make the `old` one variable, such as `db[old].rename..`? – Timo Jun 07 '22 at 10:40
17

Assume that the database name is "mytestdb" and collection name is "orders". collection name change to orders2015 The simplest way is,

> use mytestdb
> db.orders.renameCollection( "orders2015" )

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

Lakshmikandan
  • 4,301
  • 3
  • 28
  • 37
16

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")

Ev0oD
  • 1,395
  • 16
  • 33
  • `db.getCollection("oldCollectionILLEGALName").renameCollection("someBetterName")` should also work. I had renamed my collection to `_old_collection_name` and this saved me. – Liswin May 18 '21 at 14:52
  • Thanks buddy, this solved my issue. I am currently learning Mongo and in a rush I ended up naming my collection as 'employee-detail' and was stuck with the error. This was a good learning. – Azan Momin May 27 '21 at 13:16
6

In case you are using Node.js MongoDB driver:

mongoClient.db(dbName).collection('oldName').rename("newName");

https://mongodb.github.io/node-mongodb-native/3.5/api/Collection.html#rename

my case was using mongoose:

await mongoose.connection.collection("oldName").rename("newName");
arispen
  • 2,832
  • 1
  • 15
  • 13
  • Always remember, `mongo` is not `js` is not `nodejs`. So look at the difference, therefore plus one. – Timo Jun 07 '22 at 10:54
2

Rename a collection in cmd:

cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.renameCollection("someBetterName")

This example is made for MongoDB 4.2

Uku Lele
  • 514
  • 1
  • 9
  • 14
0

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!

Arun Kumar N
  • 1,611
  • 1
  • 20
  • 25