In this recipe, we will be looking at mongoexport
, a utility provided to export MongoDB data in JSON and CSV format.
You need a single node MongoDB installation, preferably with some data in it. Refer to the recipe Creating an index in Chapter 2, Understanding and Managing Indexes, for instructions on how to import sample data into a MongoDB instance.
- Execute the
mongoexport
utility with the following options:
mongoexport -d mydb -c mockdata --fields first_name,last_name,language --query '{language: "English"}' --type csv > my_data.csv
- Your output should be similar to this:
2017-10-04T16:15:51.939+0000 connected to: localhost 2017-10-04T16:15:51.981+0000 exported 866 records
- Examine the contents file
my_data.csv
:
first_name,last_name,language Gareth,Mott,English Pace,Goodram,English Valaree,Dickinson,English Nickola,Messer,English Ellene,Wardlaw,English Caryn,Petruk,English Alta,Major,English Sonya,Ritchman,English Howie,MacHostie,English...