[英]How to export Mongo-database using Docker?
I use "mongoDB" image in docker-container.我在 docker-container 中使用“mongoDB”图像。 When I run command for export DB to csv:
当我运行将 DB 导出到 csv 的命令时:
docker exec -i 418f46e5595d mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone --out ./info.csv
File saved in folder where container is working.文件保存在容器工作的文件夹中。 How can I save it on my machine?
我怎样才能把它保存在我的机器上?
My decision:我的决定:
1) export 1) 出口
docker exec -i 418f46e5595d mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone --out ./info.csv
2)copy to my machine 2)复制到我的机器
docker cp webspider_mongo_1:/data/info.csv .
remove the --out
option so it will export to stdout
and redirect output to a file, something like this:删除
--out
选项,以便它将导出到stdout
并将输出重定向到文件,如下所示:
docker exec -i 418f46e5595d mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone | gzip > info.csv
You might want to redirect error messages to a different stream, so:您可能希望将错误消息重定向到不同的流,因此:
docker exec -i 418f46e5595d mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone 2>/tmp/mongoexport.err | gzip > info.csv
Other solutions are great, but just adding an alternative also:其他解决方案很棒,但也只需添加一个替代方案:
docker-compose.yml - mount to local folder ./mongodb_data:/data/db docker-compose.yml - 挂载到本地文件夹 ./mongodb_data:/data/db
services:
mongo1:
volumes:
- ./mongodb_data:/data/db
export to relative mongo path - /data/db/info.csv - Will automatically populate into local folder - ./mongodb_data/info.csv导出到相对 mongo 路径- /data/db/info.csv - 将自动填充到本地文件夹 - ./mongodb_data/info.csv
docker exec -i 418f46e5595d mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone --out "/data/db/info.csv";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.