How to export Mongo-database using Docker?

2019-07-28 19:18发布

问题:

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?

回答1:

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


回答2:

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 .