How to export Mongo-database using Docker?

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?

Upvotes: 5

Views: 12851

Answers (3)

Inspiraller
Inspiraller

Reputation: 3806

Other solutions are great, but just adding an alternative also:

docker-compose.yml - mount to local folder ./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

docker exec -i 418f46e5595d  mongoexport --db saveInfo --collection infoobjects --type=csv --fields _id,postLink,postTitle,contactPhone --out "/data/db/info.csv";

Upvotes: 1

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 .

Upvotes: 3

Webert Lima
Webert Lima

Reputation: 14015

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

Upvotes: 5

Related Questions