How do you rename a MongoDB database?

前端 未结 10 1565
难免孤独
难免孤独 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:20

    In the case you put all your data in the admin database (you shouldn't), you'll notice db.copyDatabase() won't work because your user requires a lot of privileges you probably don't want to give it. Here is a script to copy the database manually:

    use old_db
    db.getCollectionNames().forEach(function(collName) {
        db[collName].find().forEach(function(d){
            db.getSiblingDB('new_db')[collName].insert(d); 
        }) 
    });
    

提交回复
热议问题