有什么简单的方法吗?


当前回答

在MongoDB中将一个集合(myCollection1)从一个数据库复制到另一个数据库,

**Server1:**
myHost1.com 
myDbUser1
myDbPasword1
myDb1
myCollection1

outputfile:
myfile.json 

**Server2:**
myHost2.com 
myDbUser2
myDbPasword2
myDb2
myCollection2 

你可以这样做:

mongoexport  --host myHost1.com --db myDb1 -u myDbUser1  -p myDbPasword1 --collection myCollection1   --out  myfile.json 

然后:

mongoimport  --host myHost2.com --db myDb2 -u myDbUser2  -p myDbPasword2 --collection myCollection2   --file myfile.json 

另一种情况,使用CSV文件:

Server1:
myHost1.com 
myDbUser1
myDbPasword1
myDb1
myCollection1
fields.txt
    fieldName1
    fieldName2

outputfile:
myfile.csv

Server2:
myHost2.com 
myDbUser2
myDbPasword2
myDb2
myCollection2

你可以这样做:

mongoexport  --host myHost1.com --db myDb1 -u myDbUser1  -p myDbPasword1 --collection myCollection1   --out  myfile.csv --type=csv

在CSV文件中添加列类型(name1.decimal(),name1.string()..),然后:

mongoimport  --host myHost2.com --db myDb2 -u myDbUser2  -p myDbPasword2 --collection myCollection2   --file myfile.csv --type csv --headerline --columnsHaveTypes

其他回答

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

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

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

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

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

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

在MongoDB中将一个集合(myCollection1)从一个数据库复制到另一个数据库,

**Server1:**
myHost1.com 
myDbUser1
myDbPasword1
myDb1
myCollection1

outputfile:
myfile.json 

**Server2:**
myHost2.com 
myDbUser2
myDbPasword2
myDb2
myCollection2 

你可以这样做:

mongoexport  --host myHost1.com --db myDb1 -u myDbUser1  -p myDbPasword1 --collection myCollection1   --out  myfile.json 

然后:

mongoimport  --host myHost2.com --db myDb2 -u myDbUser2  -p myDbPasword2 --collection myCollection2   --file myfile.json 

另一种情况,使用CSV文件:

Server1:
myHost1.com 
myDbUser1
myDbPasword1
myDb1
myCollection1
fields.txt
    fieldName1
    fieldName2

outputfile:
myfile.csv

Server2:
myHost2.com 
myDbUser2
myDbPasword2
myDb2
myCollection2

你可以这样做:

mongoexport  --host myHost1.com --db myDb1 -u myDbUser1  -p myDbPasword1 --collection myCollection1   --out  myfile.csv --type=csv

在CSV文件中添加列类型(name1.decimal(),name1.string()..),然后:

mongoimport  --host myHost2.com --db myDb2 -u myDbUser2  -p myDbPasword2 --collection myCollection2   --file myfile.csv --type csv --headerline --columnsHaveTypes

这并不能解决你的问题,但是mongodb shell有一个copyTo方法,可以将一个集合复制到同一个数据库中的另一个集合:

db.mycoll.copyTo('my_other_collection');

它还可以从BSON转换为JSON,所以mongodb /mongorestore是最好的方式,就像其他人说的那样。

我会滥用mongo cli mongo doc中的连接功能。这意味着您可以启动一个或多个连接。 如果您想将客户集合从test复制到同一服务器中的test2。首先你开始mongo shell

use test
var db2 = connect('localhost:27017/test2')

执行常规查找并将前20条记录复制到test2。

db.customer.find().limit(20).forEach(function(p) { db2.customer.insert(p); });

或者通过某些标准进行筛选

db.customer.find({"active": 1}).forEach(function(p) { db2.customer.insert(p); });

只需将localhost更改为IP或主机名即可连接到远程服务器。我使用它将测试数据复制到测试数据库进行测试。