有什么简单的方法吗?


当前回答

以防一些heroku用户在这里遇到麻烦,像我一样想从登台数据库复制一些数据到生产数据库,反之亦然,这里是如何非常方便地做到这一点(注意,我希望没有错字在那里,不能检查它atm。,我会尽快确认代码的有效性):

to_app="The name of the app you want to migrate data to"
from_app="The name of the app you want to migrate data from"
collection="the collection you want to copy"
mongohq_url=`heroku config:get --app "$to_app" MONGOHQ_URL`
parts=(`echo $mongohq_url | sed "s_mongodb://heroku:__" | sed "s_[@/]_ _g"`)
to_token=${parts[0]}; to_url=${parts[1]}; to_db=${parts[2]}
mongohq_url=`heroku config:get --app "$from_app" MONGOHQ_URL`
parts=(`echo $mongohq_url | sed "s_mongodb://heroku:__" | sed "s_[@/]_ _g"`)
from_token=${parts[0]}; from_url=${parts[1]}; from_db=${parts[2]}
mongodump -h "$from_url" -u heroku -d "$from_db" -p"$from_token" -c "$collection" -o col_dump
mongorestore -h "$prod_url" -u heroku -d "$to_app" -p"$to_token" --dir col_dump/"$col_dump"/$collection".bson -c "$collection"

其他回答

你可以随时使用Robomongo。从v0.8.3开始,有一个工具可以通过右键单击集合并选择“将集合复制到数据库”来完成此操作。

具体操作请参见http://blog.robomongo.org/whats-new-in-robomongo-0-8-3/

由于这个特性在0.8.5版本中被移除,所以如果你想尝试的话,你将不得不使用0.8.3或0.8.4。

有不同的方法来进行集合复制。注意,复制可以发生在同一个数据库、不同的数据库、分片数据库或mongod实例中。有些工具对于大型集合的复制是有效的。

使用$merge聚合: 将聚合管道的结果写入指定的集合。注意,复制可以跨数据库进行,甚至可以跨分片集合进行。创建一个新的集合或替换现有的集合。4.2新版功能。 例子:db.test。聚合([{$merge: {db: "newdb", coll: "newcoll"}}])

带$out的聚合: 将聚合管道的结果写入指定的集合。注意,复制只能在同一个数据库中进行。创建一个新的集合或替换现有的集合。 例子:db.test。聚合([{$out: "newcoll"}])

Mongoexport和mongoimport: 这些是命令行工具。 mongoexport生成集合数据的JSON或CSV导出。导出的输出使用mongoimport作为目标集合的源。

mongodb和mongorestore: 这些是命令行工具。 mongodb实用程序用于创建数据库或集合内容的二进制导出。mongorestore程序从mongodump创建的二进制数据库转储中加载数据到目标。

db.cloneCollection (): 将一个集合从远程mongod实例复制到当前mongod实例。 4.2版后已移除。

db.collection.copyTo (): 将所有文档从集合复制到新集合(在同一数据库中)。 3.0版后已移除。从4.2版开始,MongoDB此命令无效。

注意:除非上面的命令是从mongo shell运行的。

参考:MongoDB手册。

您也可以使用最喜欢的编程语言(如Java)或环境(如NodeJS),使用适当的驱动程序软件编写程序来执行复制-这可能涉及使用查找和插入操作或其他方法。这个查找-插入也可以从mongo shell执行。

您还可以使用像MongoDB Compass这样的GUI程序进行集合复制。

在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

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

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

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

以防一些heroku用户在这里遇到麻烦,像我一样想从登台数据库复制一些数据到生产数据库,反之亦然,这里是如何非常方便地做到这一点(注意,我希望没有错字在那里,不能检查它atm。,我会尽快确认代码的有效性):

to_app="The name of the app you want to migrate data to"
from_app="The name of the app you want to migrate data from"
collection="the collection you want to copy"
mongohq_url=`heroku config:get --app "$to_app" MONGOHQ_URL`
parts=(`echo $mongohq_url | sed "s_mongodb://heroku:__" | sed "s_[@/]_ _g"`)
to_token=${parts[0]}; to_url=${parts[1]}; to_db=${parts[2]}
mongohq_url=`heroku config:get --app "$from_app" MONGOHQ_URL`
parts=(`echo $mongohq_url | sed "s_mongodb://heroku:__" | sed "s_[@/]_ _g"`)
from_token=${parts[0]}; from_url=${parts[1]}; from_db=${parts[2]}
mongodump -h "$from_url" -u heroku -d "$from_db" -p"$from_token" -c "$collection" -o col_dump
mongorestore -h "$prod_url" -u heroku -d "$to_app" -p"$to_token" --dir col_dump/"$col_dump"/$collection".bson -c "$collection"