有什么简单的方法吗?


当前回答

这里有很多正确答案。我会选择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);
});"

其他回答

使用“Studio3T for MongoDB” 通过单击数据库,集合或特定的集合具有导出和导入工具 下载链接:https://studio3t.com/download/

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

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

您可以使用聚合框架解决您的问题

db.oldCollection.aggregate([{$out : "newCollection"}])

需要注意的是,oldCollection中的索引不会复制到newCollection中。

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

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

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

使用pymongo,你需要在同一个mongod上有两个数据库,我做了以下工作:


Db =原始数据库 Db2 =要复制到的数据库

cursor = db["<collection to copy from>"].find()
for data in cursor:
    db2["<new collection>"].insert(data)