Exporting a collection to CSV format in MongoDB
This article discusses in detail about exporting a collection to CSV format in MongoDB. We have two methods to export a collection to CSV format: Studio 3T and MongoExport.
Export a collection to CSV format in MongoDB using the Studio 3T Export Wizard
You can use Studio 3T's Export Wizard to export MongoDB collections, views, queries, query results, or individual documents to CSV, JSON, BSON/mongodump, SQL, or other collections. This article discusses exporting to CSV format only.
Open the Export Wizard
Once connected to your MongoDB database, launch the Export Wizard by clicking Export in the global toolbar.
You can also select Export by right-clicking any server, database, or collection in the connection tree (collections, buckets, views).
Alternatively, right-click anywhere on the Results tab (Collection tab, SQL Query, IntelliShell) or any input or output panel in the Aggregation Editor and select Export.
Export Source
You can export the following files from Studio 3T at any time:
- The whole series
- Entire View
- The current query result of a find() or aggregate query
- Current cursor
- Specific files
For all exports, you can select any of six actions from the toolbar:
- Save your export as a task. You can execute it on demand or schedule it for later.
- Execute the task immediately.
- You can add, edit, or delete export units.
Changing the export source
When setting up an export, you can change the export source (for example, connection, database, collection). In the Export Units tab, under Export Source, you can:
- Click to change the source in the database or connection list;
- Drag a source from the connection tree directly into the tab.
If you want to export them, you can change the query results directly in the query bar of any export unit tab find()
. The query bar validates your JSON as you type.
Change the export file path
You can also set a default destination folder on the Export Overview page. Studio 3T will remember any modifications to this route.
Exporting MongoDB collections to CSV format
Select your export source in the Export Wizard.
This screen appears if you have not selected an item in the connection tree, executed a previous query, or selected a specific document.
Next, select CSV as the export format and click Next.
Studio 3T does a partial scan of 100 documents to automatically find the fields in your collection. You can click Full Scan to identify all fields (this may take some time) or manually add missing fields by clicking Add Custom Fields.
Click the Finish button to finish. This will launch the Export Overview tab, defaulting to the Export Unit #1 – CSV tab.
There are six sections in the Export Unit - CSV tab.
- Export Source – Displays the source connection, database, and collection. You can edit a source by clicking inside the blue dashed box or by dragging it from the connection tree directly into the tab.
- Select Fields - Add or remove custom fields and check or uncheck fields to include in the export.
-
Select Destination - Choose between Clipboard or File and define the file path if needed.
- CSV Format – Configure settings such as preset, delimiter, record separator, etc.
- Other Options - Configure non-formatting settings such as how to handle null values, column headers, etc.
- Output Preview – To enlarge the CSV file preview, click the arrow button.
To configure global export options, go to the Export Overview tab.
After customizing it in the toolbar, you can start the export immediately by clicking "Go".
Adding additional export units allows you to perform multiple CSV exports at once. You can save a CSV export as a task and schedule it to be performed later to save time.
Check the Actions window in the lower left corner to see the progress of the export.
You can also see the export file directly from this page. When you right-click the export in the same window (macOS), select the Open button in File Explorer (Windows, Linux) or Show in Finder.
Export a collection to CSV format using MongoExport in MongoDB
You can also use MongoExport to export MongoDB collections, views, queries, etc. to CSV format.
Use the --fields option to export data to CSV format
MongoExport exports the data in the data collection in the students database in CSV format to the file /opt/backups/data.csv in the next section.
The mongod instance that MongoExport connects to is listening on port 27017 on localhost.
When you export in CSV format, you must determine which fields in the document you want to export. For example, the name and address fields to export are specified in the action.
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 the fields to export to CSV format
You can also provide the fields in a file containing a line-delimited list of fields for CSV export only. Only one field is allowed per line in the file.
For example, in a file called fields.txt, you could provide the field names and addresses:
name
address
Then, using --fieldFile
the option, determine which 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
--noHeaderLine
Option to exclude field names in CSV export. The following example exports the name and address fields from the students database dataset and uses to --noHeaderLine
suppress the output of field names to 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
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Date comparison in MongoDB
Publish Date:2025/04/28 Views:169 Category:MongoDB
-
This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie
Find objects between two dates in MongoDB
Publish Date:2025/04/28 Views:94 Category:MongoDB
-
In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio
Comparing Dates in MongoDB
Publish Date:2025/04/28 Views:115 Category:MongoDB
-
Date is a common field in most databases, and sometimes we need to find exact documents from a collection in MongoDB. For example, if we have a collection of orders, we might search for those documents before or after a specific date. In th
Convert string to date in MongoDB
Publish Date:2025/04/28 Views:162 Category:MongoDB
-
MongoDB is an excellent platform that is growing in popularity. Among the various features it offers, MongoDB also allows you to convert data from one type to another. This may seem like a complex function, but it is very simple to execute.
Building the MongoDB REST API
Publish Date:2025/04/28 Views:70 Category:MongoDB
-
MongoDB is a flexible and scalable document-oriented database system that is widely used for large-volume data storage. It uses documents and collections instead of the traditional rational database approach of using tables and rows. MongoD
Using ORM with MongoDB
Publish Date:2025/04/28 Views:117 Category:MongoDB
-
MongoDB introduces a NoSQL solution for data storage and management, consisting of documents represented in JSON style. Like other database systems, MongoDB can also use ORM. In this article, we will explain the concepts of ORM and general
Locking mechanism in MongoDB
Publish Date:2025/04/28 Views:95 Category:MongoDB
-
In database management systems, locking mechanisms ensure the consistency of the entire result. For example, if some writing process is in progress on the data, a read command cannot be executed at the same time. Database resources are lock
Unique Index in MongoDB
Publish Date:2025/04/28 Views:130 Category:MongoDB
-
In this article, you'll learn about unique indexes, including what they are and how to create them in MongoDB. Additionally, the process of making a user's email unique in MongoDB is briefly described. The contents of this article are as fo
Creating Indexes in MongoDB
Publish Date:2025/04/28 Views:178 Category:MongoDB
-
Indexes help resolve queries efficiently. Without indexes, MongoDB must iterate through every document in the collection to find the documents that match the query. It will waste time and require MongoDB to handle such information. Therefor