Export a MongoDB Collection to a JSON File

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 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.

Export a Collection

The following example code exports a collection from MongoDB to a JSON file:

mongoexport --db=PetHotel --collection=pets --out=data/pets.json

This exports a collection called pets from the PetHotel database to a file called pets.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.

The above example could also be written like this:

mongoexport --db=PetHotel --collection=pets --type=json --fields=_id,name,type,weight --out=data/pets.json

In this case we added the --type parameter and a --fields parameter to specify which fields to include in the JSON file.

Below is an explanation of the parameters we supplied here.

ParameterDescription
--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).
--typeSpecifies the exported file type. In this case we specify json to export it to a JSON file. json is the default value, so if we don’t specify this parameter, the file is output as a JSON file.
--fieldsSpecifies the fields that we want to export. We have the option of exporting all fields in the collection, or just some. Separate multiple field names with a comma. When exporting to JSON, specifying field names is optional (unlike with CSV).
--outSpecifies 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.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":3.0,"name":"Meow","type":"Cat","weight":7.0}
{"_id":4.0,"name":"Scratch","type":"Cat","weight":8.0}
{"_id":5.0,"name":"Bruce","type":"Bat","weight":3.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.

Less Fields

You can use the --fields parameter to specify which fields you want to export.

When you do this on JSON files, the _id field is always exported (even when you don’t include it in the list of fields to export).

Example:

mongoexport --db=PetHotel --collection=pets --fields=name,type --out=data/pets.json

In this case I specify two fields to export.

When I open up the exported file, I can see that both of those fields are exported, plus the _id field:

{"_id":1.0,"name":"Wag","type":"Dog"}
{"_id":2.0,"name":"Bark","type":"Dog"}
{"_id":3.0,"name":"Meow","type":"Cat"}
{"_id":4.0,"name":"Scratch","type":"Cat"}
{"_id":5.0,"name":"Bruce","type":"Bat"}
{"_id":6.0,"name":"Fetch","type":"Dog"}
{"_id":7.0,"name":"Jake","type":"Dog"}

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 --out=data/pets.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).

Don’t run them from the mongo shell.