How do you rename a MongoDB database?

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

    I tried doing.

    db.copyDatabase('DB_toBeRenamed','Db_newName','host') 
    

    and came to know that it has been Deprecated by the mongo community although it created the backup or renamed DB.

    WARNING: db.copyDatabase is deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation
    {
            "note" : "Support for the copydb command has been deprecated. See 
            http://dochub.mongodb.org/core/copydb-clone-deprecation",
            "ok" : 1
    }
    

    So not convinced with the above approach I had to take Dump of local using below command

    mongodump --host --db DB_TobeRenamed --out E://FileName/
    

    connected to Db.

    use DB_TobeRenamed
    

    then

    db.dropDatabase()
    

    then restored the DB with command.

    mongorestore -host hostName -d Db_NewName E://FileName/
    
    0 讨论(0)
  • 2020-11-29 15:10

    There is no mechanism to re-name databases. The currently accepted answer at time of writing is factually correct and offers some interesting background detail as to the excuse upstream, but offers no suggestions for replicating the behavior. Other answers point at copyDatabase, which is no longer an option as the functionality has been removed in 4.0. I've updated SERVER-701 with my notes and incredulity.

    0 讨论(0)
  • 2020-11-29 15:11

    Although Mongodb does not provide the rename Database command, it provides the rename Collection command, which not only modifies the collection name, but also modifies the database name.
    { renameCollection: "<source_namespace>", to: "<target_namespace>", dropTarget: <true|false> writeConcern: <document> }

    db.adminCommand({renameCollection: "db1.test1", to: "db2.test2"})
    

    This command only modifies the metadata, the cost is very small, we only need to traverse all the collections under db1, renamed to db2 to achieve rename Database name.
    you can do it in this Js script

    var source = "source";
    var dest = "dest";
    var colls = db.getSiblingDB(source).getCollectionNames();
    for (var i = 0; i < colls.length; i++) {
    var from = source + "." + colls[i];
    var to = dest + "." + colls[i];
    db.adminCommand({renameCollection: from, to: to});
    }
    

    Be careful when you use this command

    renameCollection has different performance implications depending on the target namespace.

    If the target database is the same as the source database, renameCollection simply changes the namespace. This is a quick operation.

    If the target database differs from the source database, renameCollection copies all documents from the source collection to the target collection. Depending on the size of the collection, this may take longer to complete.

    0 讨论(0)
  • 2020-11-29 15:13

    You could do this:

    db.copyDatabase("db_to_rename","db_renamed","localhost")
    use db_to_rename
    db.dropDatabase();
    

    Editorial Note: this is the same approach used in the question itself but has proven useful to others regardless.

    0 讨论(0)
  • 2020-11-29 15:18

    NOTE: Hopefully this changed in the latest version.

    You cannot copy data between a MongoDB 4.0 mongod instance (regardless of the FCV value) and a MongoDB 3.4 and earlier mongod instance. https://docs.mongodb.com/v4.0/reference/method/db.copyDatabase/

    ALERT: Hey folks just be careful while copying the database, if you don't want to mess up the different collections under single database.

    The following shows you how to rename

    > show dbs;
    testing
    games
    movies
    

    To rename you use the following syntax

    db.copyDatabase("old db name","new db name")
    

    Example:

    db.copyDatabase('testing','newTesting')
    

    Now you can safely delete the old db by the following way

    use testing;
    
    db.dropDatabase(); //Here the db **testing** is deleted successfully
    

    Now just think what happens if you try renaming the new database name with existing database name

    Example:

    db.copyDatabase('testing','movies'); 
    

    So in this context all the collections (tables) of testing will be copied to movies database.

    0 讨论(0)
  • 2020-11-29 15:20

    No there isn't. See https://jira.mongodb.org/browse/SERVER-701

    Unfortunately, this is not an simple feature for us to implement due to the way that database metadata is stored in the original (default) storage engine. In MMAPv1 files, the namespace (e.g.: dbName.collection) that describes every single collection and index includes the database name, so to rename a set of database files, every single namespace string would have to be rewritten. This impacts:

    • the .ns file
    • every single numbered file for the collection
    • the namespace for every index
    • internal unique names of each collection and index
    • contents of system.namespaces and system.indexes (or their equivalents in the future)
    • other locations I may be missing

    This is just to accomplish a rename of a single database in a standalone mongod instance. For replica sets the above would need to be done on every replica node, plus on each node every single oplog entry that refers this database would have to be somehow invalidated or rewritten, and then if it's a sharded cluster, one also needs to add these changes to every shard if the DB is sharded, plus the config servers have all the shard metadata in terms of namespaces with their full names.

    There would be absolutely no way to do this on a live system.

    To do it offline, it would require re-writing every single database file to accommodate the new name, and at that point it would be as slow as the current "copydb" command...

    0 讨论(0)
提交回复
热议问题