Skip to content

Instantly share code, notes, and snippets.

@JaniAnttonen
Last active March 18, 2024 09:58
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JaniAnttonen/90945058ed41616f96d2cfa5f131bd09 to your computer and use it in GitHub Desktop.
Save JaniAnttonen/90945058ed41616f96d2cfa5f131bd09 to your computer and use it in GitHub Desktop.
Export Docker MongoDB collection as a JSON file

First, find the container that runs your MongoDB and ssh into it.

Then, find the collection you want to export:

mongo
show dbs
use <database>
show collections
exit

Then, run mongoexport command in the shell (not in mongo):

mongoexport -d <database-name> -c <collection-name> --out output.json

Then, on your local machine, copy the file from inside the docker container to your current folder:

docker cp <mongodb docker container name>:/output.json .

Don't forget the dot from the end.

@ivan-marquez
Copy link

@JaniAnttonen I'm trying to run this exact command, but it's failing due to permissions:

error opening output stream: open output.json: permission denied

any ideas on how to fix this? Thanks!

@JaniAnttonen
Copy link
Author

Seems like you don't have write access to the output file? sudo the command or try to write to a different location. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment