Exporting MongoDb collections to JSON
Jul 23, 2017 · 1 min read
Using mongoexport, you can export your MongoDb collections into JSON format. The fastest way to get the tool is to simply pull the Mongo image, get a terminal inside the container and run the export command from there.
Since you are exporting collections it is wise to also mount the current working directory to a folder inside of the container where you are running the mongoexport command.
First, get the bash terminal to the container and mount your current folder to the /data folder inside of the container:
docker run -it -v $(PWD):/data mongo /bin/bashFinally, run the mongoexport command and get your collection(s) exported to JSON file(s):
mongoexport -h [HOST:PORT] -d [DATABASE] -c [COLLECTION] -u [USERNAME] -p [PASSWORD] -o [JSON_FILE] --jsonArrayMake sure you include --jsonArray when running the above command, to get a valid JSON document, instead of a single JSON object per line.
