The MongoDB Database Tools include a utility called mongoexport
that enables you to export MongoDB data to a CSV or JSON file.
One of the things you can do with this utitlity is export query results. This article shows you how to use mongoexport
to export MongoDB query results 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.
Example
The following example code exports the results of a query to a CSV file:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=_id,name,type,weight --query='{ "type": "Dog" }' --out=data/dogs.csv
This exports a query that queries a collection called pets
in the PetHotel
database. The query is exported to a file called dogs.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 on which to run mongoexport . 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 (or run the query against). 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 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). |
--query or -q | Specifies the query for which results we want to export. This must be enclosed in single quotes (so that it doesn’t interact with your shell). This parameter can also be passed using -q . |
--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 run the query against the original collection.
use PetHotel
db.pets.find({ "type": "Dog" })
Result:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 } { "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 } { "_id" : 6, "name" : "Fetch", "type" : "Dog", "weight" : 17 } { "_id" : 7, "name" : "Jake", "type" : "Dog", "weight" : 30 }
So we can see that there are 4 dogs, all with the same fields that we specified in our export operation.
Now let’s open up the exported file dogs.csv
to see what’s inside:
_id,name,type,weight 1,Wag,Dog,20 2,Bark,Dog,10 6,Fetch,Dog,17 7,Jake,Dog,30
OK, so all data is in the exported file as expected.
Export Less Fields
You can specify less fields with the --fields
parameter if you wish.
Example:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=name,weight --query='{ "type": "Dog" }' --out=data/dogs.csv
Resulting file:
name,weight Wag,20 Bark,10 Fetch,17 Jake,30
Remove Column Headers
You’ll notice that the exported file in the previous examples 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 --query='{ "type": "Dog" }' --noHeaderLine --out=data/dogs.csv
Now when I open up the exported file, there are no column headers:
1,Wag,Dog,20 2,Bark,Dog,10 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/dogs_fields.txt --query='{ "type": "Dog" }' --out=data/dogs.csv
The file that contains the fields must have the fields listed, one per line.
Here’s what the dogs_fields.txt
file looked like for this example:
name type weight
This resulted in the contents of the exported file looking like this:
name,type,weight Wag,Dog,20 Bark,Dog,10 Fetch,Dog,17 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 as the underlying document.
For example, this code:
mongoexport --db=PetHotel --collection=pets --type=csv --fields=type,name,weight,_id --query='{ "type": "Dog" }' --out=data/dogs.csv
Results in the following CSV file:
type,name,weight,_id Dog,Wag,20,1 Dog,Bark,10,2 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 --query='{ "type": "Dog" }' --out=data/dogs.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).