MongoDB Export

The data that has been prepared, i.e., inserted and updated might require to be pushed outside the database as a backup or for other similar purposes. MongoDB allows its users to perform that. In this chapter, you will learn about mongoexport command, which is used in MongoDB to export the data from the database.

What Does the Export of Data Mean?

Data export is either an automatic or semi-automated task that changes a file from one format to another so that it can be used externally by other applications. It involves "interpreting" a set of data from one format used in a particular application into another. Such type of interpretation process gets accomplished either automatically or through the user's intervention.

Exporting Data from MongoDB

MongoDB provides a utility called mongoexport to its users, through which users can export data from MongoDB databases to a JSON file format or a CSV file format. This utility is to be found in the MongoDB's bin subfolder (the path can be read like this: /mongodb/bin). As you run this feature, and while running, supply the database name, the name of the collection as well as the file you wish to export to, it performs the exporting task.

Exporting a MongoDB Collection to a JSON File

So, for performing the exporting of data, you have first to open a new terminal or command prompt window (cmd) and then write the appropriate command like this:

Example:
mongoexport --db techwriter --collection techwriter --out /data/dump/tw/tw.json

In case you discover that the above code is not running the mongoexport command, then it is for sure that you have either exited from the mongo utility or else you have opened a new Terminal or command prompt (cmd) window before executing this mongoexport, as it comes under a separate service. The mongoexport statement written above will assume that your MongoDB bin folder is in your PATH, but if it is not in the place, then you have to specify the full path of the mongoexport file which will be as follows: /mongodb/bin/mongoexport or whatever path you have for the established directory of MongoDB.

In case, you do not offer a path for your exported file; the file gets created wherever (the path) you are residing at the time of running the command. Moreover giving the full path, or navigating to where you want your exported data file to be will make the task neat and easy to access.

Exporting a MongoDB Collection to a CSV File

Till now, you have encountered how to export files in JSON format. Now, there is another file format which is proven to be the right way of representing data - the CSV (comma-separated value) file. For exporting your data to a CSV file, you have to add -type = csv to your command.

Example:
mongoexport --db techwriter --collection writers -type = csv --fields _id, writername --out /data/dump/tw/techwriter.csv

Also, you can identify the fields in the documents for exporting. In this example, you have use mongoexport utility for exporting the techwriter collection to a CSV file. Exporting of _id and writername fields are done here. Also, note that the file name has a .csv extension.

Additional Attributes of Mongoexport Utility

You can make use of -limit, --sort and -skip attributes to your mongoexport statement like this:

mongoexport --db techwriter --collection writers --limit 4 --out /data/dump/tw.json
mongoexport --db techwriter --collection writers --limit 3 --sort '{_id: 2}' --out /data/dump/tw_sorted.json
mongoexport --db techwriter --collection writers --limit 6 --sort '{_id: 2}' --skip 2 --out /data/dump/tw_sorted_skipped.json

Scroll Back to Top