Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

MongoDB import/export data

farenda 2017-03-31 0

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:

  • Check out MongoDB Tutorial!
  • MongoDB Reference Manual is very comprehensive!
Share with the World!
Categories MongoDB Tags mongodb
Previous: Parallel Merge Sort in Java
Next: Spring Locale from Request

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok