The MongoDB Database Tools include a utility called mongoexport
that enables you to export MongoDB data to a CSV or JSON file.
This article shows you how to use mongoexport
to export a MongoDB collection to a CSV file.
Syntax
The syntax for mongoexport
goes like this:
mongoexport --collection=<coll> <options> <connection-string>
You need to run mongoexport
commands from your system’s command line (e.g. a new Terminal or Command Prompt window).
Don’t run mongoexport
commands from the mongo
shell.
Export a Collection
The following example code exports a collection from MongoDB:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=_id,name,type,weight --out=data/pets.csv
This exports a collection called pets
from the PetHotel
database to a file called pets.csv
in the data/
folder.
If the folder doesn’t exist, it’s created. Same with the file. By the way, this assumes that there are no permission issues with writing a file to the specified location.
In this example I didn’t specify any host, port, authentication, etc, so it exports the collection from the MongoDB instance running on the default localhost port number 27017
.
Below is an explanation of the parameters we supplied here.
Parameter | Description |
---|---|
--db or -d | Specifies the database that contains the collection we want to export. In this case, the database is called PetHotel .This parameter can alternatively be passed using -d (instead of --db ). |
--collection or -c | Specifies the collection we want to export. In this case, the collection is called pets .This parameter can alternatively be passed as -c (instead of --collection ). |
--type | Specifies the exported file type. In this case we specify csv to export it to a CSV file. |
--fields | Specifies the fields that we want to export. We have the option of exporting all fields in the collection, or just some. You need to list each one here, separated by a comma. When exporting to CSV, specifying the field names is a requirement. You can do so via the --fields parameter or the --fieldFile parameter (more on that later). |
--out | Specifies the exported file name and where it will be located. If you don’t specify a file name, mongoexport writes data to standard output (stdout ). |
Check the Exported File
Let’s verify that the export operation worked as expected.
First, let’s check the original collection.
use PetHotel
db.pets.find()
Result:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 } { "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 } { "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 } { "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 } { "_id" : 5, "name" : "Bruce", "type" : "Bat", "weight" : 3 } { "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 } { "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }
So we can see that there are 7 pets, all with the same fields that we specified in our export operation.
Now let’s open up the exported file pets.csv
to see what’s inside:
_id,name,type,weight 1,Wag,Dog,20 2,Bark,Dog,10 3,Meow,Cat,7 4,Scratch,Cat,8 5,Bruce,Bat,3 6,Fetch,Dog,17 7,Jake,Dog,30
OK, so all data is in the exported file as expected.
Remove Column Headers
You’ll notice that the exported file in the previous example included the column headers.
You also have the option of exporting the file without column headers. To do this, use the --noHeaderLine
parameter.
Example:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=_id,name,type,weight --noHeaderLine --out=data/pets.csv
Now when I open up the exported file, there are no column headers:
1,Wag,Dog,20 2,Bark,Dog,10 3,Meow,Cat,7 4,Scratch,Cat,8 5,Bruce,Bat,3 6,Fetch,Dog,17 7,Jake,Dog,30
Use a File for the Field Names
You can replace the --field
parameter with the --fieldFile
parameter to specify the name of a file that contains the field names that you want to export.
mongoexport --db=PetHotel --collection=pets --type=csv --fieldFile=data/pets_fields.txt --out=data/pets.csv
The file that contains the fields must have the fields listed, one per line.
Here’s what the pets_fields.txt
file looked like for this example:
_id name type weight
This resulted in the contents of the exported file looking like this:
_id,name,type,weight 1,Wag,Dog,20 2,Bark,Dog,10 3,Meow,Cat,7 4,Scratch,Cat,8 5,Bruce,Bat,3 6,Fetch,Dog,17 7,Jake,Dog,30
Changing the Column Order
You can switch the ordering of the fields to export. They don’t have to be in the same order of the underlying document.
For example, this code:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=_id,weight,type,name --out=data/pets.csv
Results in the following CSV file:
_id,weight,type,name 1,20,Dog,Wag 2,10,Dog,Bark 3,7,Cat,Meow 4,8,Cat,Scratch 5,3,Bat,Bruce 6,17,Dog,Fetch 7,30,Dog,Jake
And the following:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=type,name,weight,_id --out=data/pets.csv
Results in this:
type,name,weight,_id Dog,Wag,20,1 Dog,Bark,10,2 Cat,Meow,7,3 Cat,Scratch,8,4 Bat,Bruce,3,5 Dog,Fetch,17,6 Dog,Jake,30,7
Access Control/Authentication
If you’re not using localhost, you can use the --host
parameter to specify the host, and --port
to specify the port. You can also use the --username
parameter to specify the user name and --password
to for the password. If you omit the password parameter, you will be prompted for it. There’s also an --authenticationDatabase
parameter for specifying the authentication database where the user has been created.
Example:
mongoexport --host=myhost.example.com --port=37017 --username=homer --authenticationDatabase=admin --db=PetHotel --collection=pets --type=csv --fields=_id,name,type,weight --out=data/pets.csv
Check for mongoexport
mongoexport
is part of the MongoDB Database Tools package. The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB.
If you’re not sure whether you have the MongoDB Database Tools/mongoexport
installed, try running the following command in your Terminal or Command Prompt to check:
mongoexport --version
If you have it, you should see version information, etc. If you don’t have it, you can use the installation instructions over at the MongoDB website to install it on to your system.
Where to Run the Commands?
Don’t forget, you need to run mongoexport
commands from your system’s command line (e.g. a new Terminal or Command Prompt window).
Don’t run them from the mongo
shell.