1024programmer Java [308] Mongo uses tools to copy databases and tables

[308] Mongo uses tools to copy databases and tables

1. Use the tool Studio 3T for MongoDB

Select a Collections collection in a database and then hold down the Ctrl+C copy shortcut key to pop up the following help box.

Click Ctrl& #43;V Paste shortcut key ,Rename the copied name.

2. Use the tool Robo 3T

Right-click in Collection and select Copy Collection

Then rename it

3. Use the foreach method to run directly in the shell

  • On the same host Copy a table from one db to a table in another db

db.collection_name.find().forEach(function(d){db.getSiblingDB(' new_database')['collection_name'].insert(d);

collection_name is the database table name
new_database It is the destination database

Clone the local collection,mongodb does not provide commands for local copy,but we can write a loop insertion method to complete

  • In the same Copy a table under the same db on the host machine

db. Copy source table name.find().forEach(function(x){db.Destination table name.insert(x );

4. Copy database


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

The following four options are optional:

  • fromdbt: source db;
  • todb: target db;
  • fromhost: host address of source db,can be omitted if it is within the same mongod instance;
  • username: if enabled Authentication mode,requires the username of the MongoDB instance on the source DB host;
  • password: Same as above,requires the password of the corresponding user;
  • mechanism: There are two mechanisms for fromhost to verify username and password: MONGODB-CR and SCRAM-SHA-1.


db.copyDatabase('test','test2','192.168. 14.52:27017','test','test','SCRAM-SHA-1')

The test without password authentication was successful,The test with password authentication was not successful



{ copydb: 1, fromhost: , fromdb: , todb: , slaveOk: , username: , nonce: ,key:

fromhost: optional,See 4.1;
slaveOK: optional&#xff0c ;Set to true,Allow data to be copied from secondary,At this time fromehost must be set;
username: Optional,See 4.1;
nonce: One-time shared key generated on the remote server;
key: hash value of password

5. Copy Collection

5.1 runCommand




copyIndexes: Whether to copy indexes

5.2 db.cloneCollection

db.cloneCollection(from, collection, query)

6. Others

Refresh disk:Write the information in the memory that has not been written to the disk,and lock it For database update operations, “but read operations can use” the runCommand command. This command can only be executed on the admin library
Format: db.runCommand({fsync:1,async :true})
async: Whether to execute asynchronously
lock:1 Lock database

Data compression: The storage structure of mongodb adopts The pre-allocation mechanism – long-term continuous operation – will leave too much fragmentation, causing the database system to become slower and slower.
The repairDatabase command is a built-in method of mongodb,It will scan all the data in the database,and reorganize the data collection through import/export,Clean up the fragments
Now look at the comparison data before and after compression,as shown below:

PRIMARY> db.t1.storageSize()
PRIMARY> db.t1.totalSize()
PRIMARY> db.repairDatabase()
{ “ok” : 1 }
PRIMARY> db.t1.storageSize()
PRIMARY> db.t1.totalSize()

Reference:https://blog.csdn.net/kturing/article/ details/73302014
https:/ /docs.mongodb.com/v3.2/reference/method/db.copyDatabase/

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/749446

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top