
1. Overview
In this article, we will let you know the steps to download a MongoDB collection as JSON.
2. MongoDB Database tools
MongoDB provides the mongoexport
utility command to download a collection as JSON that produces a?JSON?or?CSV?export of data stored in a?mongod
?instance.?This utility uses the native libmongoclient
and is most likely the fastest method.
The?mongoexport
?tool is part of the?MongoDB Database Tools?package:
The MongoDB Database Tools are a collection of command-line utilities for working with a MongoDB deployment. These tools release independently enabling us to receive more frequent updates and leverage new features as soon as they are available.?
Starting with MongoDB 4.4, the MongoDB Database Tools are now released separately from the MongoDB Server and use their own versioning, with an initial version of?100.0.0
. Previously, these tools were released alongside the MongoDB Server and used matching versioning.
2.1. Download database tools
If you are seeing the below error while running the mongoexport
MongoDB command, then those database binaries are not in your System PATH or not installed in your machine.
zsh: command not found: mongodump
You can directly download the database tools from MongoDB website or you can install the database tools using homebrew:
brew install mongodb/brew/mongodb-database-tools
Of course, make sure you have homebrew already installed.
If you still experience problems, you can check this link to install and download database tools.
3. Download MongoDB collection as JSON
Now, you are ready to use the mongoexport
command from your system command line directly and not from mongosh
shell.
mongoexport -d <database> -c <collection_name>
Options:
-o
: If you do not specify an?output file
,?mongoexport
?writes to the standard output (e.g. stdout).
--jsonArray
: generates a valid JSON document, instead of one JSON object per line.
--pretty
: outputs formatted JSON
So, you can combine the above options to download the MongoDB collection as JSON.
mongoexport --db <database-name> --collection <collection-name> --out output.json --pretty
3.1. Download collection as JSON with URI
To specify a host and/or port of the MongoDB instance, you can either:
- When using the?
--uri=<connection string
>, we can specify the database as part of the string.
mongoexport --uri="mongodb://mongodb0.example.com:27017/reporting" --collection=<collection-name> --out=output.json --pretty
2. Specify the hostname and port in the?--host
:
mongoexport --host="mongodb0.example.com:27017" --collection=<collection-name> --db=<database-name> --out=output.json --pretty
3. Specify the hostname and port in the?--host
?and?--port
:
mongoexport --host="mongodb0.example.com" --port=27017 --collection=<collection-name> --db=<database-name> --out=output.json --pretty
3.3. Type Fidelity
If you need to preserve all rich?BSON?data types when using?mongoexport
?to perform full instance backups, be sure to specify?canonical mode?to the?--jsonFormat
?option to?mongoexport
, in the following fashion:
mongoexport --jsonFormat=canonical --collection=<coll> <connection-string>
If?--jsonFormat
?is unspecified,?mongoexport
?outputs data in?Relaxed mode?by default. This relaxed mode can lose type information during the conversion.
4. Conclusion
To sum up, we have learned to download the MongoDB collection as JSON. To learn more about MongoDB, refer to these articles.