o7planning

Importing and Exporting MongoDB Database

  1. Overview
  2. Import/Export Collection
  3. Import/Export Database

1. Overview

MongoDB provides you with 2 ways:
  • mongoexport/mongoimport
  • mongodump/mongostore
mongoexport: Being used to export data from a Collection to a file (json, csv,..)
mongoimport: Being used to import data to a Collection from a file (json, csv,..)
Collection is the concept of MongoDB, which is equivalent to Table concepts in relational databases (Oracle, SQL Server, MySQL, ..).
mongodump: Being used to export all datas of a database to files (Put in a folder), including some files (bson, json)
mongostore: Being used to import all datas to a database from dump directory (Product of mongodump)

2. Import/Export Collection

mongoexport
# Export to json
mongoexport -d database_name - c collection_name -o outfile.json


# Export to file csv
mongoexport --csv -o /tmp/people.csv -d school -c people -f firstName,lastName,telephone,email
mongoimport
# Import from json file
mongoimport -d database_name -c collection_name outfile.json



# Import from csv file
# --headerline: Using the first row of data as the column name of the Collection.
mongoimport -d database_name -c collection_name --type csv --file locations.csv --headerline
mongoexport/mongoimport and options
In a general situation, you have options for import/export, see in the following table:
Option
Meaning
Example
--help
produce help message
-v [ --verbose ]
be more verbose (include multiple times for more verbosity e.g. -vvvvv)
-h [ --host ] arg
mongo host to connect to ("left,right" for pairs)
--port arg
server port. (Can also use --host hostname:port)
--ipv6
enable IPv6 support (disabled by default)
-d [ --db ] arg
database to use
-c [ --collection ] arg
collection to use (some commands)
-u [ --username ] arg
username
-p [ --password ] arg
password
--dbpath arg
directly access mongod data files in the given path,instead of connecting to a mongod instance - needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path
--directoryperdb
if dbpath specified, each db is in a separate directory
-f [ --fields ] arg
comma seperated list of field names e.g. -f name,age
--fieldFile arg
file with fields names - 1 per line
--ignoreBlanks
if given, empty fields in csv and tsv will be ignored
--type arg
type of file to import. default: json (json,csv,tsv)
--file arg
file to import from; if not specified stdin is used
--drop
drop collection first
--headerline
CSV,TSV only - use first line as headers
--upsert
insert or update objects that already exist
--upsertFields arg
comma-separated fields for the query part of the upsert. You should make sure this is indexed.
--stopOnError
stop importing at the first error rather than continuing
--jsonArray
load a json array, not one item per line. Currently limited to 4MB.

3. Import/Export Database

mongodump used to export the entire database to a directory.
mongostore used to import the all datas to a database from a directory (export product of mongodump).
mongodump
# The syntax to export the entire database to a directory (Includes some files)

mongodump -d database_name -o output_directory
Example:
Export myfirstdb database to directory: C:/test
cd C:\DevPrograms\MongoDB\bin
mongodump -d myfirstdb -o C:/test
The result, subfolder myfirstdb created in the directory C:/test, it contains some files.
mongorestore
# The simplest syntax to import an entire database.

mongorestore -d database_name path_to_database
For example, the folder C:/test/myfirstdb containing the dump files. We will use it to import into the database: mydb2
cd C:\DevPrograms\MongoDB\bin
mongorestore -d mydb2 C:\test\myfirstdb
View on visual tools RoboMongo: