How can I rename a collection in MongoDB?

42,398

Solution 1

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

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
42,398
Aaron Silverman
Author by

Aaron Silverman

SVP Engineering at Benefix.us. Co-creator of Doodle Or Die. Formerly Head of Engineering at stock media startup Storyblock, Senior Software Engineer at energy efficiency start-up Opower, and Lead Software Engineer at business analytic startup Applied Predictive Technologies. Washington, DC transplant to beautiful Ouray, Colorado via Philadelphia. Father to three children. Owner of two rescued dogs. Rock and Ice Climber. Instrument-rated private pilot.

Updated on March 14, 2021

Comments

  • Aaron Silverman
    Aaron Silverman over 3 years

    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?

  • nav
    nav over 12 years
    hint: try using tab complete to find things you think might be there ;)
  • AlexP
    AlexP over 9 years
    +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('alreadyExistingCollNam‌​e', true)
  • user2810081
    user2810081 over 7 years
    after the rename, will the indexes and everything be kept?
  • Liswin
    Liswin about 3 years
    db.getCollection("oldCollectionILLEGALName").renameCollectio‌​n("someBetterName") should also work. I had renamed my collection to _old_collection_name and this saved me.
  • Azan Momin
    Azan Momin about 3 years
    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.
  • Timo
    Timo about 2 years
    Answer from 2015 still valid as of today, plus one.
  • Timo
    Timo about 2 years
    How can I make the old one variable, such as db[old].rename..?
  • Timo
    Timo about 2 years
    Always remember, mongo is not js is not nodejs. So look at the difference, therefore plus one.