如何导出 MongoDB 中的所有集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11255630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export all collections in MongoDB?
提问by aboutstudy
I want to export all collections in MongoDB by the command:
我想通过以下命令导出 MongoDB 中的所有集合:
mongoexport -d dbname -o Mongo.json
The result is:
No collection specified!
结果是:
没有指定集合!
The manual says, if you don't specify a collection, all collections will be exported.
However, why doesn't this work?
手册说,如果您不指定集合,则将导出所有集合。
但是,为什么这不起作用?
http://docs.mongodb.org/manual/reference/mongoexport/#cmdoption-mongoexport--collection
http://docs.mongodb.org/manual/reference/mongoexport/#cmdoption-mongoexport--collection
My MongoDB version is 2.0.6.
我的 MongoDB 版本是 2.0.6。
回答by Mentor Reka
For lazy people, use mongodump
, it's faster:
对于懒惰的人,使用mongodump
,它会更快:
mongodump -d <database_name> -o <directory_backup>
And to "restore/import" it (from directory_backup/dump/):
并“恢复/导入”它(来自 directory_backup/dump/):
mongorestore -d <database_name> <directory_backup>
This way, you don't need to deal with all collections individually. Just specify the database.
这样,您就无需单独处理所有集合。只需指定数据库。
Note that I would recommend against using mongodump
/mongorestore
for big data storages. It is very slow and once you get past 10/20GB of data it can take hours to restore.
请注意,我建议不要将mongodump
/mongorestore
用于大数据存储。它非常慢,一旦超过 10/20GB 的数据,可能需要数小时才能恢复。
回答by Boris Pavlovic
I wrote bash script for that. Just run it with 2 parameters (database name, dir to store files).
我为此编写了 bash 脚本。只需使用 2 个参数(数据库名称,存储文件的目录)运行它。
#!/bin/bash
if [ ! ]; then
echo " Example of use: 1. mongodump -d dbname -o dumpname -u username -p password
2. scp -r user@remote:~/location/of/dumpname ./
3. mongorestore -d dbname dumpname/dbname/ -u username -p password
database_name [dir_to_store]"
exit 1
fi
db=
out_dir=
if [ ! $out_dir ]; then
out_dir="./"
else
mkdir -p $out_dir
fi
tmp_file="fadlfhsdofheinwvw.js"
echo "print('_ ' + db.getCollectionNames())" > $tmp_file
cols=`mongo $db $tmp_file | grep '_' | awk '{print }' | tr ',' ' '`
for c in $cols
do
mongoexport -d $db -c $c -o "$out_dir/exp_${db}_${c}.json"
done
rm $tmp_file
回答by AnoopGoudar
Follow the steps below to create a mongodump from the server and import it another server/local machine which has a username and a password
按照以下步骤从服务器创建 mongodump 并将其导入另一台具有用户名和密码的服务器/本地机器
mongorestore -d database_name directory_backup_where_mongodb_tobe_restored
回答by Usman
Exporting all collections using mongodump use the following command
使用 mongodump 导出所有集合使用以下命令
mongodump -d database_name -o directory_to_store_dumps
mongodump -d database_name -o directory_to_store_dumps
To restore use this command
要恢复使用此命令
mongodump --host xx.xxx.xx.xx --port 27017 --db your_db_name --username your_user_name --password your_password --out /target/folder/path
回答by Ravichandran K
Please let us know where you have installed your Mongo DB ? (either in Ubuntu or in Windows)
请让我们知道您在哪里安装了 Mongo DB?(在 Ubuntu 或 Windows 中)
For Windows:
- Before exporting you must connect to your Mongo DB in cmd prompt and make sure that you are able to connect to your local host.
- Now open a new cmd prompt and execute the below command,
mongodump --db database name --out path to save
eg: mongodump --db mydb --out c:\TEMP\op.json- Visit https://www.youtube.com/watch?v=hOCp3Jv6yKofor more details.
For Ubuntu:
- Login to your terminal where Mongo DB is installed and make sure you are able to connect to your Mongo DB.
- Now open a new terminal and execute the below command,
mongodump -d database name -o file name to save
eg: mongodump -d mydb -o output.json- Visit https://www.youtube.com/watch?v=5Fwd2ZB86ggfor more details .
对于 Windows:
- 在导出之前,您必须在 cmd 提示符下连接到您的 Mongo DB,并确保您能够连接到本地主机。
- 现在打开一个新的 cmd 提示符并执行以下命令,
mongodump --db 数据库名称 --out 保存路径
例如:mongodump --db mydb --out c:\TEMP\op.json对于 Ubuntu:
- 登录到安装了 Mongo DB 的终端,并确保您能够连接到您的 Mongo DB。
- 现在打开一个新终端并执行以下命令,
mongodump -d 数据库名称 -o 要保存的文件名
例如:mongodump -d mydb -o output.json
回答by karthikdivi
Previous answers explained it well, I am adding my answer to help in case you are dealing with a remote password protected database
以前的答案很好地解释了它,如果您正在处理远程密码保护的数据库,我正在添加我的答案以提供帮助
mongoexport -h id.mongolab.com:60599 -u username -p password -d mydb -c mycollection -o mybackup.json
回答by Anup_Tripathi
In case you want to connect a remote mongoDB server like mongolab.com, you should pass connection credentials eg.
如果你想连接像 mongolab.com 这样的远程 mongoDB 服务器,你应该传递连接凭据,例如。
out = `mongo #{DB_HOST}/#{DB_NAME} --eval "printjson(db.getCollectionNames())"`
collections = out.scan(/\".+\"/).map { |s| s.gsub('"', '') }
collections.each do |collection|
system "mongoexport --db #{DB_NAME} --collection #{collection} --host '#{DB_HOST}' --out #{collection}_dump"
end
回答by KailuoWang
You can use mongo --eval 'printjson(db.getCollectionNames())'
to get the list of collections
and then do a mongoexport on all of them.
Here is an example in ruby
您可以使用mongo --eval 'printjson(db.getCollectionNames())'
获取集合列表,然后对所有集合进行 mongoexport。这是 ruby 中的一个例子
mongo "{YOUR SERVER}/{YOUR DATABASE}" --eval "rs.slaveOk();db.getCollectionNames()" --quiet>__collections.txt
for /f %%a in ('type __collections.txt') do @set COLLECTIONS=%%a
for %%a in (%COLLECTIONS%) do mongoexport --host {YOUR SERVER} --db {YOUR DATABASE} --collection %%a --out data\%%a.json
del __collections.txt
回答by adamb0mb
I needed the Windows batch script version. This thread was useful, so I thought I'd contribute my answer to it too.
我需要 Windows 批处理脚本版本。这个线程很有用,所以我想我也可以贡献我的答案。
##代码##I had some issues using set /p COLLECTIONS=<__collections.txt
, hence the convoluted for /f
method.
我在使用时遇到了一些问题set /p COLLECTIONS=<__collections.txt
,因此采用了复杂的for /f
方法。
回答by mark
If you are OK with the bson format, then you can use the mongodump utility with the same -d flag. It will dump all the collections to the dump directory (the default, can be changed via the -o option) in the bson format. You can then import these files using the mongorestore utility.
如果您对 bson 格式没问题,那么您可以使用具有相同 -d 标志的 mongodump 实用程序。它将以 bson 格式将所有集合转储到转储目录(默认值,可以通过 -o 选项更改)。然后,您可以使用 mongorestore 实用程序导入这些文件。