How do i change a db name in mongodb? Simple question but i can't find anything online on how to do this. I don't want to have to rebuild my entire database, i just simple want to change the name of it. thanks.
            Asked
            
        
        
            Active
            
        
            Viewed 2.0k times
        
    10
            
            
        - 
                    Just be careful while renaming you must not mess up with copying the collections to existing database. Checkout my answer https://stackoverflow.com/a/44986052/1696621 – Channaveer Hakari Jul 08 '17 at 12:08
4 Answers
19
            
            
        Simple use this :
db.copyDatabase("old_db_name","new_db_name","localhost")
use old_db_name
db.dropDatabase();
- 
                    Just be careful while renaming you must not mess up with copying the collections to existing database. Checkout my answer https://stackoverflow.com/a/44986052/1696621 – Channaveer Hakari Jul 08 '17 at 12:07
- 
                    1
10
            The only way is to clone the database to one of a different name
http://www.mongodb.org/display/DOCS/Copy+Database+Commands
You could go here and vote for that feature
 
    
    
        kelloti
        
- 8,705
- 5
- 46
- 82
4
            
            
        Starting in version 4.2, MongoDB removes the deprecated copydb command. Also, db.copyDatabase() and db.cloneDatabase() can only be run when connected to MongoDB 4.0 or earlier.
Use this approach for version 4.2 as mentioned at mongoDB manual
- Use mongodump to dump the test database to an archive mongodump-test-db:
mongodump --archive="mongodump-test-db" --db=test
- Use mongorestore with --nsFrom and --nsTo to restore (with database name change) from the archive:
mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'
 
    
    
        Hemant
        
- 1,403
- 2
- 11
- 21
3
            
            
        You can dump database
./mongodump -d mydb
rename folder
mv /path/to/dump/folder/mydb /path/to/dump/folder/mynewdb
and then restore
./mongorestore
 
    
    
        Alexander Serkin
        
- 1,679
- 1
- 12
- 11
 
    