Java Programming Tutorials

Java programming tutorials with many code examples!

MongoDB import/export data

MongoDB import/export data can be performed using many different tools. In this post we show how to use them to move MongoDB data around!

This article has been written based on MongoDB 3.4, so things may change over time.

Load data from the mongo shell

If you are in the mongoshell and want to import some data, then the simplest approach is to use the load(path) function in the mongoshell, that will load data from a JSON file:

load("/full/path/to/sample-data.json")

Mongo Import – import JSON data

Note that mongoimport and mongoexport use data in JSON format, whereas MongoDB stores them in BSON, which is superset of JSON. Thus mongoimport and mongoexport don’t reliably preserve all BSON data types, therefore should not be used for production backups, but they are ok for testing and playing with MongoDB.

Import data into selected database and collection

mongoimport --db mydb --collection mycoll --file mydata.json

If you omit collection parameter, then target collection name will be taken from the filename (without file extension):

# Imports into "customers" collection:
mongoimport --db mydb --file customers.json

Import using shorter syntax:

mongoimport -d mydb -c mycoll < mydata.json

Mongo Export – export JSON/CSV data

mongoexport has mostly the same parameters as mongoimport.

Export selected collection as JSON

mongoexport --db mycoll --collection mycoll --out mydata.json

Export selected collection as CSV

mongoexport --db mycoll --collection mycoll --out mydata.csv --type=csv

Export only documents matching query:

mongoexport --db sales --collection contacts --query '{"city": "Cracow"}'

Export data from remote MongoDB host

mongoexport --host mongodb1.mycorp.kam --port 37017 --username user
            --password pass --collection contacts --out mdb1-examplenet.json

Mongo dump – export BSON data

mongodump allows to export data in BSON format, so preserving all extended type information. A nice feature is that you can interrupt the dump after sometime and have only partial export. It’s very handy when you want to export only snapshot of data from large production MongoDB and later import onto your laptop.

Dump selected collection into a directory

This will create a dump directory and output all data there:

mongodump -d mydb -c flights

Dump selected database into file archive

mongodump -d mydb --archive=mydb.20170331.archive

Dump into compressed archive file

mongodump --archive=flights.20170101.gz --gzip --db Flights

Dump from remote Mongo server

mongodump --host emghlc1534 --port 30002 --username UUU --password XXX
          --authenticationDatabase admin

Dump selected data into file archive

mongodump --host emghlc1534 --port 30002 --username UUU --password XXX
          --authenticationDatabase admin --archive=mydata.20170331.archive
          -d mydb -c Flights
          --query="{departureDate: {$gte: ISODate(\"2017-01-01T00:00:00.000Z\")}}"

Mongo restore – restore BSON data

mongorestore restores BSON data exported with mongodump. It’s syntax is very simple: mongorestore [dump], where dump is a path to dump directory created by mongodump.

Restore data from default “dump” directory

mongorestore --db=mydb --collection=Flight

Restore from given directory

mongorestore --db=mydb --dir dump-20170101/

Restore data from archive file

mongorestore --db=mydb --collection=Flight --archive=mydata.20170331.archive

Restore compressed Mongo archive

mongorestore --gzip --archive=mydata.20170101.gz --db mydb

Dump and restore in one step

mongodump --archive --db Flights --host prod.corpo.com --port 27017 | mongorestore --archive

References:

Share with the World!