MongoDB - Export Data

In MongoDB, you can export data using the mongoexport utility.

You can use the mongoexport utility to export data from your MongoDB database, to a JSON or CSV file.

The utility is located in the MongoDB bin directory (eg, /mongodb/bin). When you run the utility, provide the name of the database, the collection, and the file you want it to be exported to.

To export data, first open a new Terminal/Command Prompt window, then type the applicable command.

Export a Collection to a JSON File

Here, we use mongoexport to export the artists collection to a JSON file:

Resulting message:

2016-07-12T09:57:37.613+0700	connected to: localhost
2016-07-12T09:57:37.614+0700	exported 13 records

Resulting file:

{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}
{"_id":{"$oid":"5781c9ac48ef8c6b3ffb014a"},"artistname":"Jorn Lande"}
{"_id":1.0,"artistname":"AC/DC"}
{"_id":{"$oid":"5781d7f248ef8c6b3ffb014d"},"artistname":"The Kooks"}
{"_id":{"$oid":"5781d7f248ef8c6b3ffb014e"},"artistname":"Bastille"}
{"_id":{"$oid":"5781d7f248ef8c6b3ffb014f"},"artistname":"Gang of Four"}
{"_id":{"$oid":"5781f85d48ef8c6b3ffb0150"},"artistname":"Deep Purple","albums":[{"album":"Machine Head","year":1972.0,"genre":"Rock"},{"album":"Stormbringer","year":1974.0,"genre":"Rock"}]}
{"_id":{"$oid":"578214f048ef8c6b3ffb0159"},"artistname":"Miles Davis","albums":[{"album":"Kind of Blue","year":1959.0,"genre":"Jazz"},{"album":"Bitches Brew","year":1970.0,"genre":"Jazz"}]}
{"_id":{"$oid":"578217c248ef8c6b3ffb015a"},"artistname":"Robben Ford","albums":[{"album":"Bringing it Back Home","year":2013.0,"genre":"Blues"},{"album":"Talk to Your Daughter","year":1988.0,"genre":"Blues"}]}
{"_id":{"$oid":"578217c248ef8c6b3ffb015b"},"artistname":"Snoop Dogg","albums":[{"album":"Tha Doggfather","year":1996.0,"genre":"Rap"},{"album":"Reincarnated","year":2013.0,"genre":"Reggae"}]}
{"_id":2.0,"artistname":"Prince","address":{"street":"Audubon Road","city":"Chanhassen","state":"Minnesota","country":"United States"}}
{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}
{"_id":4.0,"artistname":"Rush"}

If you find that you can't run mongoexport, be sure that you've either exited the mongo utility, or opened a new Terminal/Command Prompt window before running mongoexport, as it is a separate utility.

The above command assumes that the MongoDB bin directory is in your PATH. If it's not, you will need to use the full path to the mongoexport file. For example, /mongodb/bin/mongoexport or wherever your MongoDB deployment is installed.

If you don't provide a file path for the exported file, it will be created wherever you are located when you run the command. Either provide the full path, or navigate to where you'd like the data file to be written before you run the command.

Export a Collection to a CSV File

To export to a CSV file, add --type=csv to the command.

You must also specify the fields in the MongoDB documents to export.

Here, we use mongoexport to export the artists collection to a CSV file. We export the _id and artistname fields. We've also given the file name a .csv extension.

Resulting message:

2016-07-12T10:16:33.111+0700	connected to: localhost
2016-07-12T10:16:33.114+0700	exported 13 records

Resulting CSV file:

_id,artistname
ObjectId(5780fbf948ef8c6b3ffb0149),The Tea Party
ObjectId(5781c9ac48ef8c6b3ffb014a),Jorn Lande
1,AC/DC
ObjectId(5781d7f248ef8c6b3ffb014d),The Kooks
ObjectId(5781d7f248ef8c6b3ffb014e),Bastille
ObjectId(5781d7f248ef8c6b3ffb014f),Gang of Four
ObjectId(5781f85d48ef8c6b3ffb0150),Deep Purple
ObjectId(578214f048ef8c6b3ffb0159),Miles Davis
ObjectId(578217c248ef8c6b3ffb015a),Robben Ford
ObjectId(578217c248ef8c6b3ffb015b),Snoop Dogg
2,Prince
3,Moby
4,Rush

Export the results of a Query

You can use the --query option to specify a query to export. The query must be enclosed in single quotes.

Here, we export details on Miles Davis to a JSON file:

Resulting message:

2016-07-12T10:32:19.794+0700	connected to: localhost
2016-07-12T10:32:19.795+0700	exported 1 record

Resulting JSON file:

{"_id":{"$oid":"578214f048ef8c6b3ffb0159"},"artistname":"Miles Davis","albums":[{"album":"Kind of Blue","year":1959.0,"genre":"Jazz"},{"album":"Bitches Brew","year":1970.0,"genre":"Jazz"}]}

Other Options

The mongoexport utility provides a number of options. Here are some potentially useful ones.

The --limit Option

Limits the number of documents in the export.

Resulting file:

{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}
{"_id":{"$oid":"5781c9ac48ef8c6b3ffb014a"},"artistname":"Jorn Lande"}
{"_id":1.0,"artistname":"AC/DC"}

The --sort Option

Specifies how the results are ordered.

Here, we sort the file by the _id field in ascending order (i.e. 1). To make it descending, use a -1.

Resulting file:

{"_id":1.0,"artistname":"AC/DC"}
{"_id":2.0,"artistname":"Prince","address":{"street":"Audubon Road","city":"Chanhassen","state":"Minnesota","country":"United States"}}
{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}

The --skip Option

Allows you to instruct mongoexport to skip a number of documents before starting the export operation.

Resulting file:

{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}
{"_id":4.0,"artistname":"Rush"}
{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}

The --pretty Option

Outputs documents in a more readable JSON format.

Resulting file:

{
	"_id": {
		"$oid": "578214f048ef8c6b3ffb0159"
	},
	"artistname": "Miles Davis",
	"albums": [
		{
			"album": "Kind of Blue",
			"year": 1959.0,
			"genre": "Jazz"
		},
		{
			"album": "Bitches Brew",
			"year": 1970.0,
			"genre": "Jazz"
		}
	]
}