1. Home
  2. MongoDB
  3. MongoDB Tutorial
  4. 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 database in the local instance as alternatives for rename database.

Syntax:

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

Examples:

Copy Database on Same Instance

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

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

Output:

{ "ok" : 1 }

Also you can use 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.

Tags , ,