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 JSON 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 JSON file:
mongoexport --db=PetHotel --collection=pets --query='{ "type": "Dog" }' --out=data/dogs.json
This exports a query that queries a collection called pets
in the PetHotel
database. The query is exported to a file called dogs.json
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
.
You can also use the --type
parameter to explicitly specify JSON. The default is JSON, so this is optional when exporting to JSON.
You can also include a --fields
parameter to specify which fields to export. By default, it exports all fields when using JSON. However, when exporting to CSV, you must specify which fields to export.
Here’s an example of the above example with these two parameters added:
mongoexport --db=PetHotel --collection=pets --type=json --fields=_id,name,type,weight --query='{ "type": "Dog" }' --out=data/dogs.json
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 json to export it to a JSON file. Given JSON is the default value, this parameter is optional when exporting to JSON. |
--fields | Specifies the fields that we want to export. We have the option of exporting all fields or just some. When exporting to JSON, specifying the field names is optional (it’s a requirement when exporting to CSV). |
--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.json
to see what’s inside:
{"_id":1.0,"name":"Wag","type":"Dog","weight":20.0} {"_id":2.0,"name":"Bark","type":"Dog","weight":10.0} {"_id":6.0,"name":"Fetch","type":"Dog","weight":17.0} {"_id":7.0,"name":"Jake","type":"Dog","weight":30.0}
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 --fields=name,weight --query='{ "type": "Dog" }' --out=data/dogs.json
Resulting file:
{"_id":1.0,"name":"Wag","weight":20.0} {"_id":2.0,"name":"Bark","weight":10.0} {"_id":6.0,"name":"Fetch","weight":17.0} {"_id":7.0,"name":"Jake","weight":30.0}
You’ll notice that the _id
field was included in the exported file, even though I didn’t explicitly include it in the --fields
argument. That is because the _id
field is always included when exporting to JSON – even when you don’t explicitly include it. This isn’t the case when exporting to CSV.
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 --query='{ "type": "Dog" }' --out=data/dogs.json
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).