How do you rename a MongoDB database?

前端 未结 10 1554
难免孤独
难免孤独 2020-11-29 15:04

There\'s a typo in my MongoDB database name and I\'m looking to rename the database.

I can copy and delete like so...

db.copyDatabase(\'old_name\', \         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 15:32

    From version 4.2, the copyDatabase is deprecated. From now on we should use: mongodump and mongorestore.

    Let's say we have a database named: old_name and we want to rename it to new_name.

    First we have to dump the database:

    mongodump --archive="old_name_dump.db" --db=old_name
    

    If you have to authenticate as a user then use:

    mongodump -u username --authenticationDatabase admin \
              --archive="old_name_dump.db" --db=old_name
    

    Now we have our db dumped as a file named: old_name_dump.db.

    To restore with a new name:

    mongorestore --archive="old_name_dump.db" --nsFrom="old_name.*" --nsTo="new_name.*"
    

    Again, if you need to be authenticated add this parameters to the command:

    -u username --authenticationDatabase admin 
    

    Reference: https://docs.mongodb.com/manual/release-notes/4.2-compatibility/#remove-support-for-the-copydb-and-clone-commands

提交回复
热议问题