有什么简单的方法吗?


当前回答

这里有很多正确答案。我会选择mongodump和mongorestore作为一个大的收藏:

mongodump --db fromDB --gzip --archive | mongorestore --drop --gzip --archive --nsFrom "fromDB.collectionName" --nsTo "toDB.collectionName"

虽然如果我想快速复制,它很慢,但它是有效的:

use fromDB 
db.collectionName.find().forEach(function(x){
   db.getSiblingDB('toDB')['collectionName'].insert(x);
});"

其他回答

我通常会这样做:

use sourcedatabase;
var docs=db.sourcetable.find();
use targetdatabase;
docs.forEach(function(doc) { db.targettable.insert(doc); });

我知道这个问题已经被回答了,但我个人不会做@JasonMcCays的回答,因为光标流的事实,这可能会导致无限的游标循环,如果集合仍在使用。相反,我将使用snapshot():

http://www.mongodb.org/display/DOCS/How+to+do+Snapshotted+Queries+in+the+Mongo+Database

@bens的回答也很好,不仅适用于集合的热备份,而且mongorestore不需要共享同一个mongod。

从现有的MongoDB atlas cluster DB导入数据最简单的方法是使用MongoDB & mongorestore命令。

要从现有的DB中创建转储,您可以使用:

mongodump --uri="<connection-uri>"

还有其他连接选项,可以在这里查找:https://www.mongodb.com/docs/database-tools/mongodump/

在dump/目录中成功创建转储后,您可以使用导入数据到您的其他db,如下所示:

mongorestore --uri="<connection-uri-of-other-db>" <dump-file-location>

类似地,对于mongorestore,还有其他连接选项,可以在命令中查找以恢复特定的集合: https://www.mongodb.com/docs/database-tools/mongorestore/

转储文件的位置将在转储目录中。可能存在与您转储的DB名称相同的子目录。例如,如果您转储了测试数据库,那么转储文件的位置将是/dump/test

这可能只是一个特殊情况,但对于一个包含100k文档的集合,其中包含两个随机字符串字段(长度为15-20个字符),使用哑mapreduce几乎是find-insert/copyTo速度的两倍:

db.coll.mapReduce(function() { emit(this._id, this); }, function(k,vs) { return vs[0]; }, { out : "coll2" })

这里有很多正确答案。我会选择mongodump和mongorestore作为一个大的收藏:

mongodump --db fromDB --gzip --archive | mongorestore --drop --gzip --archive --nsFrom "fromDB.collectionName" --nsTo "toDB.collectionName"

虽然如果我想快速复制,它很慢,但它是有效的:

use fromDB 
db.collectionName.find().forEach(function(x){
   db.getSiblingDB('toDB')['collectionName'].insert(x);
});"