How to Export Collection Into a CSV Format in MongoDB

Tahseen Tauseef Feb 02, 2024
  1. Use Studio 3T Export Wizard to Export Collection Into a CSV Format in MongoDB
  2. Use MongoExport to Export Collection Into a CSV Format in MongoDB
How 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.

Export to CSV - Export Wizard 1

You may also pick Export by right-clicking on any server, database, or collection in the Connection Tree (Collections, Buckets, Views).

Export to CSV - Export Wizard 2

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 to CSV - Export Wizard 3

Export Sources

You may export the following files from Studio 3T at any time:

  1. Entire collections
  2. Entire views
  3. The current query result of a find() or aggregation query
  4. The current cursor
  5. Specific documents

Export to CSV - Export Wizard 4

For all exports, you can choose any of the six actions from the toolbar:

  1. Save the exports as a task. You can execute it on-demand or schedule it for later.
  2. Execute the task immediately.
  3. You can add, edit, or remove the export units.

Export to CSV - Export Wizard 5

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:

  1. Click to change the source from your list of databases or connections;
  2. Drag the source from the Connection Tree directly into the tab.

Export to CSV - Export Wizard 6

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.

Export to CSV - Export Wizard 7

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 to CSV - Export Wizard 8

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.

Export to CSV - Export Wizard 9

Next, choose CSV as your export format, then click Next.

Export to CSV - Export Wizard 10

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.

Export to CSV - Export Wizard 11

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.

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

  2. Select fields – Add or remove custom fields and check or uncheck fields to be included in the export.

  3. Select target – Choose between clipboard or file, and define the file path as needed.

    Export to CSV - Export Wizard 12

  4. CSV format – Configure the settings like preset, delimit, record separator, etc.

  5. Other options – Configure the non-formatting settings like how to treat null values, column headers, etc.

  6. Output preview – To enlarge the CSV file preview, click the arrow button.

Export to CSV - Export Wizard 13

To configure the global export options, go to the Export Overview tab.

Export to CSV - Export Wizard 14

After customizing it in the toolbar, you may instantly launch the export by clicking Execute.

Export to CSV - Export Wizard 15

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

Export to CSV - Export Wizard 16

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

Related Article - MongoDB CSV