How to Export Collection Into a CSV Format in MongoDB
- Use Studio 3T Export Wizard to Export Collection Into a CSV Format in MongoDB
- Use MongoExport to Export Collection Into a CSV Format in MongoDB
This article discusses exporting collection into a CSV format in MongoDB in detail. We have two ways to export a collection into a CSV format: Studio 3T and MongoExport.
Use Studio 3T Export Wizard to Export Collection Into a CSV Format in MongoDB
You may export MongoDB collections, views, queries, query results, or single documents to CSV, JSON, BSON/mongodump, SQL, or another collection using Studio 3T’s Export Wizard. This article only discusses exporting to the CSV format.
Open the Export Wizard
Start the Export Wizard by clicking on Export in the Global Toolbar once you’ve connected to a MongoDB database.
You may also pick Export by right-clicking on any server, database, or collection in the Connection Tree (Collections, Buckets, Views).
Alternatively, right-click anywhere in Aggregation Editor on a Result tab (Collection Tab, SQL Query, IntelliShell) or any input or output panel, and select Export.
Export Sources
You may export the following files from Studio 3T at any time:
- Entire collections
- Entire views
- The current query result of a
find()
or aggregation query - The current cursor
- Specific documents
For all exports, you can choose any of the six actions from the toolbar:
- Save the exports as a task. You can execute it on-demand or schedule it for later.
- Execute the task immediately.
- You can add, edit, or remove the export units.
Change the Export Source
When setting your export, you can change the export source (e.g., connection, database, collection). In the Export Unit tab, under Export Source, you can:
- Click to change the source from your list of databases or connections;
- Drag the source from the Connection Tree directly into the tab.
You may change the find()
query results directly in the Query bar of any Export Unit tab if you’re exporting them. As you type, the Query bar verifies your JSON.
Change the Export File Path
You can also set the default target folder on the Export Overview page. Studio 3T will remember any modifications to this route.
Export MongoDB Collection Into a CSV Format
Select your export source in the Export Wizard.
This screen will show if you haven’t selected an item in the Connection Tree, executed a prior query, or selected particular documents.
Next, choose CSV as your export format, then click Next.
Studio 3T does a partial scan of 100 documents to find fields in your collection automatically. You may either click on Full scan to identify all fields (which may take some time) or manually add missing fields by clicking on Add Custom Field.
Finish by clicking the Finish button. This will launch the Export Overview tab, which will default to the Export Unit #1 – CSV tab.
There are six sections in the Export Unit – CSV tab.
-
Export source – The source connection, database, and collection are displayed. You may edit this by clicking within the blue dotted box or dragging the source from the Connection Tree into the tab directly.
-
Select fields – Add or remove custom fields and check or uncheck fields to be included in the export.
-
Select target – Choose between clipboard or file, and define the file path as needed.
-
CSV format – Configure the settings like preset, delimit, record separator, etc.
-
Other options – Configure the non-formatting settings like how to treat null values, column headers, etc.
-
Output preview – To enlarge the CSV file preview, click the arrow button.
To configure the global export options, go to the Export Overview tab.
After customizing it in the toolbar, you may instantly launch the export by clicking Execute.
Adding extra export units allows you to conduct many CSV exports at once. You may save your CSV export as a task and schedule it to execute later to save time.
Check the Operations window in the bottom-left corner to see how far your export has progressed.
You may also see the export file straight from this page. Choose the Open button in File Explorer (Windows, Linux) or Reveal in Finder when right-clicking on export in the same window (macOS).
Use MongoExport to Export Collection Into a CSV Format in MongoDB
You may also export MongoDB collections, views, queries, etc., to CSV format using MongoExport.
Export Data Into a CSV Format Using the --fields
Option
MongoExport exports data in CSV format from the data
collection in the students
database to the file /opt/backups/data.csv
in the following section.
The mongod
instance to which MongoExport connects is listening on port 27017 on localhost.
You must determine the fields in the documents to export when exporting in CSV format. For example, the name
and address
fields to export are specified in operation.
mongoexport --db=students --collection=data --type=csv --fields=name,address --out=/opt/backups/data.csv
Output:
name, address
Jack Reacher, 456 Example Road
Peter Parker, 123 Sample Street
Use a File to Specify Fields to Export Into a CSV Format
You may also provide fields in a file containing a line-separated list of fields to export for CSV exports only. Only one field per line is allowed in the file.
In a file called fields.txt
, for example, you can provide the fields name
and address
:
name
address
Then, using the --fieldFile
option, determine the fields to export with the file:
mongoexport --db=students --collection=data --type=csv --fieldFile=fields.txt --out=/opt/backups/data.csv
Exclude Field Names From CSV Output
The --noHeaderLine
option can exclude field names in a CSV export. The following example exports the name
and address
fields in the data
collection in the students
database and uses --noHeaderLine
to suppress the output of the field names as the first line:
mongoexport --db=students --collection=data --type=csv --fields=name,address --noHeaderLine --out=/opt/backups/data.csv
Output:
Jack Reacher, 456 Example Road
Peter Parker, 123 Sample Street