MongoDB Copy Database

MongoDB Copy Database

 MongoDB Copy Database


Use copyDatabsae() function to copy MongoDB database from remote database instance to local database instance. You can also use this to copy the database in the local instance as an alternative for renaming the database.

Syntax:

db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism)

Examples:

Copy Database on Same Instance

You can create a duplicate database in the same instance by using copyDatabase() function.

db.copyDatabase("olddb","newdb")

Output:

{ "ok" : 1 }

Also, you can use the above option to rename database in MongoDB. But you must have enough space to create a copy of an existing database with a different name. After that, you can remove the old database to release space.

Copy Database from Remote Instance

You can also use copyDatabase() function to copy the database from remote MongoDB instance to local database instance.

db.copyDatabase("remote_dbname","local_dbname","10.8.0.2","username","password")

The above command will show a large output on your screen as per available documents in the database.

Reactions

Post a Comment

0 Comments

close