How to export Mongo-database using Docker?

2019-07-28 19:38发布

I use "mongoDB" image in docker-container. When I run command for export DB to 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?

2条回答
地球回转人心会变
2楼-- · 2019-07-28 19:55

My decision:
1) export
docker exec -i 418f46e5595d mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone --out ./info.csv
2)copy to my machine
docker cp webspider_mongo_1:/data/info.csv .

查看更多
Summer. ? 凉城
3楼-- · 2019-07-28 19:57

remove the --out option so it will export to stdout and redirect output to a file, something like this:

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
查看更多
登录 后发表回答