数据备份/恢复

mongodump && mongorestore

mongodump -h host:port -d db_source -c collection_source -o dump/collection_source.bson
mongorestore -h host:port -d db_target -c collection_target dump/collection_source.bson

mongoexport && mongoimport

mongoexport -h [ip_address] -d [database] -c [collection] > source.json
mongoimport -h [ip_address] -d [database] -c [target] source.json


mongoexport --collection=events --db=reporting --out=events.json

3.2 之后的版本, 支持管道,即时处理

mongoexport -h [ip_address] -d [database] -c [collection] | mongoimport -h [ip_address] -d [database] -c [target]

数据少,也可以用命令行

use db_source;
var docs = db_source.collection_souce.find();
use db_target;
docs.forEach(function(d){db.collection_target.insert(d)});

db.source.find().forEach(function(doc) {
  db.target.insert(doc);
});

参考链接

https://segmentfault.com/q/1010000004949649
https://docs.mongodb.com/database-tools/mongodump/
https://docs.mongodb.com/database-tools/mongorestore/
https://docs.mongodb.com/database-tools/mongoexport/
https://docs.mongodb.com/database-tools/mongoimport/