1. 复制数据库
1.1 db.copyDatabase(fromdb,todb,fromhost,username,password,mechanism)
后面四个选项可选:
- fromhost: 源db的主机地址,如果在同一个mongod实例内可以省略;
- username: 如果开启了验证模式,需要源DB主机上的MongoDB实例的用户名;
- password: 同上,需要对应用户的密码;
- mechanism: fromhost验证username和password的机制,有:MONGODB-CR、SCRAM-SHA-1两种。
1.2 db.runCommand()
{ copydb: 1,
fromhost: <hostname>,
fromdb: <database>,
todb: <database>,
slaveOk: <bool>,
username: <username>,
nonce: <nonce>,
key: <key>
}
fromhost: 可选,见1.1;
slaveOK: 可选,设置为true,允许从secondary复制数据,此时fromehost必须被设置;
username: 可选,见1.1;
nonce: 远程服务器上产生的一次性共享密钥;
key: 对password的hash值
2. 复制Collection
2.1 runCommand
db.runCommand({
cloneCollection: <namespace>
fromhost: <hostname>
query: <filter>
});
db.runCommand({cloneCollection:"testdb.testcol", fromhost:"192.168.1.12:27017", query:{"age":{"gt":2}}});
2.2 db.cloneCollection
db.cloneCollection(from, collection, query)
参考:
https://docs.mongodb.com/manual/reference/method/db.copyDatabase/#db.copyDatabase
https://docs.mongodb.com/manual/reference/command/copydb/
https://docs.mongodb.com/manual/reference/command/cloneCollection/
https://docs.mongodb.com/manual/reference/method/db.cloneCollection/#db.cloneCollection
https://docs.mongodb.com/manual/reference/command/clone/