Exporting MongoDb collections to JSON

Peter Jausovec
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/bash

Finally, 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] --jsonArray

Make sure you include --jsonArray when running the above command, to get a valid JSON document, instead of a single JSON object per line.

Peter Jausovec

— Kubernetes — Docker — Go — Python — React —

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade