Dump Mongo Collection into JSON format

138,472

Solution 1

Mongo includes a mongoexport utility (see docs) which can dump a collection. This utility uses the native libmongoclient and is likely the fastest method.

mongoexport -d <database> -c <collection_name>

Also helpful:

-o: write the output to file, otherwise standard output is used (docs)

--jsonArray: generates a valid json document, instead of one json object per line (docs)

--pretty: outputs formatted json (docs)

Solution 2

Use mongoexport/mongoimport to dump/restore a collection:

Export JSON File:

mongoexport --db <database-name> --collection <collection-name> --out output.json

Import JSON File:

mongoimport --db <database-name> --collection <collection-name> --file input.json

WARNING mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.

Also, http://bsonspec.org/

BSON is designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.

In addition to compactness, BSON adds additional data types unavailable in JSON, notably the BinData and Date data types.

Solution 3

Here's mine command for reference:

mongoexport --db AppDB --collection files --pretty --out output.json

On Windows 7 (MongoDB 3.4), one has to move the cmd to the place where mongod.exe and mongo.exe file resides => C:\MongoDB\Server\3.4\bin else it won't work saying it does not recongnize mongoexport command.

Solution 4

From the Mongo documentation:

The mongoexport utility takes a collection and exports to either JSON or CSV. You can specify a filter for the query, or a list of fields to output

Read more here: http://www.mongodb.org/display/DOCS/mongoexport

Share:
138,472
Parvin Gasimzade
Author by

Parvin Gasimzade

Currently, I'm a Sr.Software Development Engineer at Amazon, London/UK Tech: Java, Spring, Nosql, MongoDB, Solr, ElasticSearch, Python, Big Data, Spark/EMR, AWS Linkedin Profile

Updated on April 28, 2022

Comments

  • Parvin Gasimzade
    Parvin Gasimzade about 2 years

    Is there any way to dump mongo collection into json format? Either on the shell or using java driver.I am looking for the one with best performance.

    • Max Peng
      Max Peng over 4 years
      mongoexport -d <database> -c <collection_name> --out <xxx.json> --pretty --host <host> --port <port> --username <user> --authenticationDatabase admin You can specify the host, port, username, password like this and the default authentication database is admin.
  • Reimund
    Reimund about 10 years
    Use the -d flag to specify what database to use.
  • Max Truxa
    Max Truxa about 8 years
    If you want pretty printed JSON (e.g. to inspect a collection during development) use the --pretty flag: mongoexport -d mydatabase -c mycollection --pretty
  • Prasad
    Prasad almost 7 years
    As a matter of fact it is still dumping bson and metadata.bson :-(
  • What Would Be Cool
    What Would Be Cool over 6 years
    If Mongo is located on a different host, here's an example from the Mongo doc mongoexport --host mongodb1.example.net --port 37017 --username user --password "pass" --collection contacts --db marketing --out mdb1-examplenet.json
  • andrewdotn
    andrewdotn over 6 years
    Is there an example of what “rich BSON data” would not survive a mongoexport/mongoimport round trip?
  • Priyanshu Chauhan
    Priyanshu Chauhan over 6 years
    It adds support for data types like Date and binary that aren't supported in JSON. Also, faster to encoding and decoding bsonspec.org
  • Christian Dechery
    Christian Dechery over 6 years
    This answer is incorrect. mongodump outputs data in BSON format. Other answers correctly referred to mongoexport as the right tool.
  • conor909
    conor909 almost 5 years
    Does anyone know of a formatter that will format normal json to the silly "single line no comma" format that MongoDB expects when importing?
  • shyamzzp
    shyamzzp almost 5 years
    This is working for me, its outputing all the collection into a tmp folder with bson and json files. I used mongodump -d {dbname} -o tmp
  • icedwater
    icedwater almost 5 years
    It seems like --pretty is gone as of version 2.6.10.
  • ᐅdevrimbaris
    ᐅdevrimbaris almost 3 years
    I guess this method exports in Mongodb extended JSON format. For example dates are like ISODATE("2021-12-...."). I had problems parsing this format with Apache Drill. I don't know if you can override the export to output in well-known JSON.
  • Oliver Dixon
    Oliver Dixon over 2 years
    We have loads of collections, possible to just dump all collections?