{"id":105804,"date":"2025-03-05T00:28:59","date_gmt":"2025-03-05T00:28:59","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=105804"},"modified":"2025-02-25T00:57:29","modified_gmt":"2025-02-25T00:57:29","slug":"importing-and-exporting-data-in-mongodb-compass","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/nosql\/mongodb\/importing-and-exporting-data-in-mongodb-compass\/","title":{"rendered":"Importing and exporting data in MongoDB Compass"},"content":{"rendered":"\n<p><strong>This article is part of Robert Sheldon's continuing series on Mongo DB. To see all of the items in the series, <a href=\"https:\/\/www.red-gate.com\/simple-talk\/collections\/robert-sheldon-ongoing-mongodb-primer\/\">click here<\/a>.<\/strong><\/p>\n\n\n\n\n<p>When working with MongoDB, you\u2019ll likely need to import or export document data from time-to-time. You can use MongoDB Compass for both types of operations, working with either the JavaScript Object Notation (JSON) format or the comma-separated values (CSV) format.<\/p>\n\n\n\n<p>Both formats are commonly used to exchange data across different types of systems and applications. In Compass, you can import documents from JSON or CSV files, or you can export them in either format. You can also export an entire collection, a subset of data from a collection, or the data returned by an aggregation.<\/p>\n\n\n\n<p>Earlier in this series, I briefly demonstrated how to use MongoDB Compass to import data from a JSON file into a target collection. In this article, I go deeper into the process of importing documents and then explain how to export documents to an external file. Compass provides an intuitive interface for importing and exporting data, making the process both quick and easy. This article provides multiple examples for better understanding how these operations work.<\/p>\n\n\n\n<p>Note: For the examples in this article, I used the same MongoDB Atlas environment I used for the previous articles in this series. Refer to the first article for details about setting up these environments. The examples are based on the <code>hr<\/code> database and <code>candidates<\/code> collection. If you plan to try out these examples, the database and collection should already exist. In addition, the collection should contain no schema validation rules or documents. You\u2019ll also need to download the <code>candidates.csv<\/code> file and the <code>candidates.json<\/code> file <a href=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/MongoDbSheldonImportingExportingFiles.zip\">from here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-import-data-from-a-csv-file-into-a-mongodb-collection\">Import data from a CSV file into a MongoDB collection<\/h2>\n\n\n\n<p>Comma-separated values (CSV) files are commonly used to transfer data between systems, applications, and individuals. It does not matter how the data is generated or used, as long as it shares a common format.<\/p>\n\n\n\n<p>Strictly speaking, a CSV file is a text file that uses a comma as its delimiter. However, MongoDB Compass uses the term <em>CSV file<\/em> in a broader sense. To this end, the interface supports four delimiter types: comma, tab, semicolon, and space. In addition, the file does not necessarily need to take a <code>.csv<\/code> extension. For example, you might use a file that has a<code>.txt<\/code> extension.<\/p>\n\n\n\n<p>Regardless of the delimiter used, the file must follow a specific format. To begin with, the file\u2019s header row must list the document fields, separated by commas or by one of the other supported delimiters. The header row should contain all possible fields, even if some documents don\u2019t include those fields.<\/p>\n\n\n\n<p>Each subsequent row in the file should list a document, or more specifically, the document\u2019s values, specified in the same order as the fields in the header row. The values should also be separated by the same delimiter used for the header row.<\/p>\n\n\n\n<p>The following figure shows the contents of the <code>candidates.csv<\/code> file, which you\u2019ll be importing into the <code>candidates<\/code> collection. The first row lists the field names, and the remaining rows list the field values, with each document on a separate line. A comma is used as the delimiter.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1742\" height=\"488\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-1.png\" alt=\"\" class=\"wp-image-105807\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-1.png 1742w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-1-300x84.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-1-1024x287.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-1-768x215.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-1-1536x430.png 1536w\" sizes=\"auto, (max-width: 1742px) 100vw, 1742px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>When specifying the field names, you must take into account fields that are objects (embedded documents) and fields that are arrays. In this case, all the documents include the <code>position<\/code> field, which is an embedded document that contains the <code>title<\/code> and <code>dept<\/code> subfields. Many documents also include the <code>skills<\/code> field, which is an array that can contain a varying number of values.<\/p>\n\n\n\n<p>To help you better understand the type of documents in the <code>candidates.csv<\/code> file, the following code shows the first document, formatted as JSON:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n  \"_id\": 101,\n  \"name\": \"Drew\",\n  \"position\": {\n    \"title\": \"Senior Developer\",\n    \"dept\": \"R&amp;D\"\n  },\n  \"yrs_exp\": 18,\n  \"skills\": [\n    \"Java\",\n    \"SQL\",\n    \"Python\",\n    \"PHP\"\n  ]\n}<\/pre>\n\n\n\n<p>The document includes the <code>position<\/code> field, which contains the two subfields. In the CSV file, the embedded field names are listed as <code>position.title<\/code> and <code>position.dept<\/code>, indicating that they\u2019re both subfields in the <code>position<\/code> field.<\/p>\n\n\n\n<p>This document also includes the <code>skills<\/code> array, which contains four values. In the CSV file, the <code>skills<\/code> field is listed multiple times in the header row, with each value specified with an index number, as in <code>skills[0]<\/code> or <code>skills[1]<\/code>. The documents in the CSV file that include the <code>skills<\/code> array contain up to six values within the array, so the index values range from <code>0<\/code> to <code>5<\/code>. (An array\u2019s index is 0-based.)<\/p>\n\n\n\n<p>When a CSV file includes documents that don\u2019t contain a field listed in the header row, the delimiter must still be included so the number of values associated with each document matches the number of fields listed in the header row, in the order the field names are specified. If one of the fields is an array, a document must include a delimiter for each possible array value listed in the header line.<\/p>\n\n\n\n<p>For example, the following code shows the first three documents included in the <code>candidates.csv<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">101,Drew,Senior Developer,R&amp;D,18,Java,SQL,Python,PHP,,\n102,Parker,Data Scientist,R&amp;D,14,Python,SQL,R,Scala,Java,Julia\n103,Harper,Marketing Manager,Marketing,22,,,,,,<\/pre>\n\n\n\n<p>The first document includes the <code>skills<\/code> array, but the array contains only four values, so the document is listed with two trailing commas. The commas serve as placeholders for the remaining two array values. However, the <code>skills<\/code> array in the second document includes six values, so no extra commas are needed. The third document does not include the <code>skills<\/code> array, so six commas are required to correspond to the array\u2019s number of potential values.<\/p>\n\n\n\n<p>You might have noticed that the documents in the CSV file include an <code>_id<\/code> field. If you import documents that don\u2019t include this field, MongoDB will automatically add the field to each document and generate a unique value for that document.<\/p>\n\n\n\n<p>Once your CSV file is in the proper format, you can import it into your MongoDB collection. To import the data from the <code>candidates.csv<\/code> file into the <code>candidates<\/code> collection, you should first open the collection in the main window of the Compass interface. From there, click the <strong>Add<\/strong> <strong>Data<\/strong> drop-down arrow near the top of the window and then click <strong>Import JSON or CSV file<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2002\" height=\"1064\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-2.png\" alt=\"\" class=\"wp-image-105808\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-2.png 2002w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-2-300x159.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-2-1024x544.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-2-768x408.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-2-1536x816.png 1536w\" sizes=\"auto, (max-width: 2002px) 100vw, 2002px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>When your system\u2019s file manager window appears, navigate to the folder that contains the <code>candidates.csv<\/code> file, select the file, and click <strong>Select<\/strong>. Compass will launch the <strong>Import<\/strong> dialog box, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1946\" height=\"1578\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-3.png\" alt=\"\" class=\"wp-image-105809\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-3.png 1946w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-3-300x243.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-3-1024x830.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-3-768x623.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-3-1536x1246.png 1536w\" sizes=\"auto, (max-width: 1946px) 100vw, 1946px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The default delimiter is <strong>Comma<\/strong>, but you can instead select <strong>Tab<\/strong>, <strong>Semicolon<\/strong>, or <strong>Space<\/strong>. The source file will determine which delimiter you should use. The dialog box also includes two options that you can clear or select:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><strong>Ignore empty strings.<\/strong> If selected, MongoDB will drop fields that contain empty strings, rather than including them. This option is selected by default.<\/li>\n\n\n\n<li><strong>Stop on errors.<\/strong> If selected, MongoDB will not import any documents if it detects an error. Otherwise, MongoDB will insert the documents that are processed up to the point that the error is detected. This option is unselected by default.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>You can also select which fields to include, and you can select different data types, if appropriate. For this example, I cleared the <strong>skills[]<\/strong> checkbox, which means that the <code>skills<\/code> field will not be included in the imported documents. I also left the data types as defined.<\/p>\n\n\n\n<p>After you\u2019ve made your selections, click <strong>Import<\/strong>. Compass will then import the data into the <code>candidates<\/code> collection and display the documents in the main Compass window, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1942\" height=\"768\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-4.png\" alt=\"\" class=\"wp-image-105810\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-4.png 1942w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-4-300x119.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-4-1024x405.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-4-768x304.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-4-1536x607.png 1536w\" sizes=\"auto, (max-width: 1942px) 100vw, 1942px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this case, the documents are displayed in Table View, but you can display them in any view, and you can work with them just like any other documents in a collection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-import-data-from-a-json-file-into-a-mongodb-collection\">Import data from a JSON file into a MongoDB collection<\/h2>\n\n\n\n<p>Now that you\u2019ve seen how to import a CSV file, let\u2019s look at how to import a JSON file. For this, you\u2019ll be using the <code>candidates.json<\/code> file, which contains the same documents as the <code>candidates.csv<\/code> file, only in the JSON format.<\/p>\n\n\n\n<p>Before you import the JSON data, you should first remove the existing documents in the <code>candidates<\/code> collection (just to keep things simple for this article). To delete all the documents, click <strong>Delete<\/strong> in the main window. When the <strong>Delete<\/strong> dialog box appears, click the <strong>Delete 10 documents<\/strong> button and then click the button again. You should also refresh the <strong>Documents<\/strong> tab to verify that all the documents have been deleted.<\/p>\n\n\n\n<p>The following code shows the first four documents as they appear in the <code>candidates.json<\/code> file. Before you import any data, you should verify that the data is in the correct format, unless you\u2019re already certain of its validity.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n  \"_id\": 101,\n  \"name\": \"Drew\",\n  \"position\": {\n    \"title\": \"Senior Developer\",\n    \"dept\": \"R&amp;D\"\n  },\n  \"yrs_exp\": 18,\n  \"skills\": [\n    \"Java\",\n    \"SQL\",\n    \"Python\",\n    \"PHP\"\n  ]\n},\n{\n  \"_id\": 102,\n  \"name\": \"Parker\",\n  \"position\": {\n    \"title\": \"Data Scientist\",\n    \"dept\": \"R&amp;D\"\n  },\n  \"yrs_exp\": 14,\n  \"skills\": [\n    \"Python\",\n    \"SQL\",\n    \"R\",\n    \"Scala\",\n    \"Java\",\n    \"Julia\"\n  ]\n},\n{\n  \"_id\": 103,\n  \"name\": \"Harper\",\n  \"position\": {\n    \"title\": \"Marketing Manager\",\n    \"dept\": \"Marketing\"\n  },\n  \"yrs_exp\": 22\n},\n{\n  \"_id\": 104,\n  \"name\": \"Darcy\",\n  \"position\": {\n    \"title\": \"Senior Developer\",\n    \"dept\": \"R&amp;D\"\n  },\n  \"yrs_exp\": 6,\n  \"skills\": [\n    \"Java\",\n    \"Csharp\",\n    \"Python\",\n    \"R\"\n  ]\n},<\/pre>\n\n\n\n<p>Although the code shows only four documents, the <code>candidates.json<\/code> file includes all 10 of the documents you saw in the CSV example. To import those documents, open the <code>candidates<\/code> collection in the main window of the Compass interface (if it\u2019s not already open). Next, click the <strong>Add<\/strong> <strong>Data<\/strong> drop-down arrow and then click <strong>Import JSON or CSV file<\/strong>.<\/p>\n\n\n\n<p>When the file manager window appears, navigate to the folder that contains the <code>candidates.json<\/code> file, select the file, and click <strong>Select<\/strong>. Compass will launch the <strong>Import<\/strong> dialog box, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1228\" height=\"688\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-5.png\" alt=\"\" class=\"wp-image-105811\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-5.png 1228w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-5-300x168.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-5-1024x574.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-5-768x430.png 768w\" sizes=\"auto, (max-width: 1228px) 100vw, 1228px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The only option available to a JSON import is the <strong>Stop on errors<\/strong> checkbox, which you can select or leave unselected. You should then click the <strong>Import<\/strong> button to pull the data into the <code>candidates<\/code> collection. The data will be displayed in the main window, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2370\" height=\"794\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6.png\" alt=\"\" class=\"wp-image-105812\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6.png 2370w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6-300x101.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6-1024x343.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6-768x257.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6-1536x515.png 1536w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-6-2048x686.png 2048w\" sizes=\"auto, (max-width: 2370px) 100vw, 2370px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>After you import the data, you can work with the documents just like you could when you imported the CSV data. The main difference is that some of the documents now include the <code>skills<\/code> array, which we did not include with our CSV import.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-export-a-mongodb-collection-to-a-json-file\">Export a MongoDB collection to a JSON file<\/h2>\n\n\n\n<p>In MongoDB Compass, you can export a collection\u2019s documents to either a JSON file or CSV file. That said, MongoDB documentation recommends that you avoid exporting to a CSV file because \u201cCSV files may lose type information and are not suitable for backing up your data.\u201d The Compass interface provides a similar warning.<\/p>\n\n\n\n<p>Because of these warnings, the rest of this article focuses exclusively on exporting data to JSON files. If you still want to export your data to a CSV file, refer to the MongoDB article <a href=\"https:\/\/www.mongodb.com\/docs\/compass\/current\/import-export\/\">Import and Export Data<\/a> for more details about exporting to the CSV format.<\/p>\n\n\n\n<p>To demonstrate how to export data to a JSON file, we\u2019ll again use the <code>candidates<\/code> collection, only this time, it should now contain the data imported in the previous section. Be sure to reopen the collection if you\u2019ve already closed it.<\/p>\n\n\n\n<p>To export the collection\u2019s documents, click the <strong>Export<\/strong> <strong>Data<\/strong> drop-down arrow and then click <strong>Export the full collection<\/strong>. (I\u2019ll discuss the other option\u2014<strong>Export query results<\/strong>\u2014in the next section.)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1336\" height=\"528\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-7.png\" alt=\"\" class=\"wp-image-105813\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-7.png 1336w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-7-300x119.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-7-1024x405.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-7-768x304.png 768w\" sizes=\"auto, (max-width: 1336px) 100vw, 1336px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>When you click the option, Compass launches the <strong>Export<\/strong> dialog box, where you can choose the export file type. You have two options: <strong>JSON<\/strong> (the default) or <strong>CSV<\/strong>. If <strong>JSON<\/strong> is selected, you can also select the JSON format type. To access the format options, click the <strong>Advanced JSON Format<\/strong> down arrow, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1216\" height=\"1192\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-8.png\" alt=\"\" class=\"wp-image-105814\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-8.png 1216w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-8-300x294.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-8-1024x1004.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-8-768x753.png 768w\" sizes=\"auto, (max-width: 1216px) 100vw, 1216px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The dialog box provides three JSON format options:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><strong>Default Extended JSON.<\/strong> This format preserves all BSON type information. This option is selected by default.<\/li>\n\n\n\n<li><strong>Relaxed Extended JSON.<\/strong> This format prioritizes readability and interoperability over type preservation. MongoDB documentation warns again using this option because it might not preserve data integrity.<\/li>\n\n\n\n<li><strong>Canonical Extended JSON.<\/strong> This format prioritizes type preservation over readability and interoperability. It ensures that data can be converted from a canonical format to BSON in most situations.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>As you can see in the figure, each format option includes examples of how different values might be rendered. For this example, we\u2019ll stick with the default option, <strong>Default Extended JSON<\/strong>.<\/p>\n\n\n\n<p>When you\u2019re ready to export the collection\u2019s documents, click <strong>Export<\/strong>. You\u2019ll then be presented with the file manager window. Navigate to the folder where you want to save the file, type a file name (or use the default), and click <strong>Select<\/strong>. MongoDB will create the file in the designated folder. The following figure shows the first five documents, as they appear in the new file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"1758\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-9.png\" alt=\"\" class=\"wp-image-105815\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-9.png 602w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-9-103x300.png 103w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-9-351x1024.png 351w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-9-526x1536.png 526w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>If you had exported the data to a JSON format other than the default, you might see different values. For example, the first document shows the <code>_id<\/code> field and its value as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"_id\": 101<\/pre>\n\n\n\n<p>However, if you had used the Canonical Extended JSON format, the field and its value would appear as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"_id\": {\n  \"$numberInt\": \"101\"\n},<\/pre>\n\n\n\n<p>When exporting document data to JSON, you should fully understand the differences in the format options and how they can potentially impact your data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-export-query-results-to-a-json-file\">Export query results to a JSON file<\/h2>\n\n\n\n<p>In some cases, you might not want to export all the documents in a collection, only a specific subset of them. You can achieve this by defining a query in the Compass interface and then exporting the query results to a JSON or CSV file. (If you\u2019re not familiar with how to define a query in Compass, refer to the article <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/nosql\/mongodb\/querying-mongodb-documents\/\">Querying MongoDB Documents<\/a>.)<\/p>\n\n\n\n<p>To export a subset of the <code>candidates<\/code> collection to a JSON file, ensure that the collection is still open and then type the following code in the <strong>Filter<\/strong> text box, which is in the query section near the top of the <strong>Documents<\/strong> tab:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ $or: [ { \"position.dept\": \"R&amp;D\" }, { \"position.dept\": \"IT\" } ] }<\/pre>\n\n\n\n<p>The code specifies that a document must contain a <code>position.dept<\/code> value of <code>R&amp;D<\/code> or <code>IT<\/code> for it to be included in the query results. Next, in the <strong>Project<\/strong> text box, type the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ name: 1, position: 1, yrs_exp: 1 }<\/pre>\n\n\n\n<p>This code specifies that only the <code>name<\/code>, <code>position<\/code>, and <code>yrs_exp<\/code> fields should be included in the results. MongoDB will also automatically include the <code>_id<\/code> field.<\/p>\n\n\n\n<p>After you define your query, click the <strong>Find<\/strong> button to display the query results in the bottom pane. There should be six documents in the results, as shown in the following figure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2376\" height=\"1118\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10.png\" alt=\"\" class=\"wp-image-105816\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10.png 2376w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10-300x141.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10-1024x482.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10-768x361.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10-1536x723.png 1536w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-10-2048x964.png 2048w\" sizes=\"auto, (max-width: 2376px) 100vw, 2376px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>To export the documents in the query results, click the <strong>Export<\/strong> <strong>Data<\/strong> drop-down arrow, and then click <strong>Export query results<\/strong>, which launches the <strong>Export<\/strong> dialog box. Ensure that <strong>JSON<\/strong> is selected as the export file type, and then click <strong>Export<\/strong>.<\/p>\n\n\n\n<p>When the file manager window appears, navigate to the folder where you want to save the file, type a file name (or use the default), and click <strong>Select<\/strong>. MongoDB will create the file in the designated folder. The following figure shows the documents as they appear in the new file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"1604\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-11.png\" alt=\"\" class=\"wp-image-105817\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-11.png 668w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-11-125x300.png 125w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-11-426x1024.png 426w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-11-640x1536.png 640w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As you can see, the file contains six documents formatted according to the JSON standard. Notice that each document contains a <code>position.dept<\/code> value of <code>R&amp;D<\/code> or <code>IT<\/code>. In addition, each document includes only the <code>_id<\/code>, <code>name<\/code>, <code>position<\/code>, and <code>yrs_exp<\/code> fields.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-export-aggregate-results-to-a-json-file\">Export aggregate results to a JSON file<\/h2>\n\n\n\n<p>MongoDB Compass also lets you export the results returned by an aggregation. To export the data, you must first open the collection (if it\u2019s not already open), go to the <strong>Aggregations<\/strong> tab, and define and run your aggregation. You can then export the aggregation\u2019s results. (If you\u2019re not familiar with how to create aggregations in Compass, refer to the article <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/nosql\/mongodb\/building-mongodb-aggregations-in-mongodb-compass\/\">Building MongoDB Aggregations in MongoDB Compass<\/a>.)<\/p>\n\n\n\n<p>To demonstrate how to export aggregated data, we\u2019ll again use the <code>candidates<\/code> collection. For the aggregation itself, we\u2019ll use the following definition:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[\n  {\n    $match: {\n      $or: [\n        {\n          \"position.dept\": \"R&amp;D\"\n        },\n        {\n          \"position.dept\": \"IT\"\n        }\n      ]\n    }\n  },\n  {\n    $group: {\n      _id: {\n        dept: \"$position.dept\"\n      },\n      total: {\n        $count: {}\n      }\n    }\n  },\n  {\n    $project: {\n      _id: 0,\n      dept: \"$_id.dept\",\n      total: 1\n    }\n  }\n]<\/pre>\n\n\n\n<p>The aggregation, includes the following three stages:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><code><strong>$match<\/strong><\/code><strong>.<\/strong> The stage filters out all documents in the pipeline except those with a <code>position.dept<\/code> value of <code>R&amp;D<\/code> or <code>IT<\/code>.<\/li>\n\n\n\n<li><code><strong>$group<\/strong><\/code><strong>.<\/strong> The stage groups the pipeline\u2019s documents based on the <code>position.dept<\/code> values. Because the documents include only the of <code>R&amp;D<\/code> or <code>IT<\/code> departments, the pipeline will now contain only two groups. This stage also calculates the total number of candidates in each group.<\/li>\n\n\n\n<li><code><strong>$project<\/strong><\/code><strong>.<\/strong> The stage removes the <code>_id<\/code> field from each group and assigns the name <code>dept<\/code> to the <code>_id.dept<\/code> field.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>When you plug these stage definitions into the Compass interface, the <strong>Aggregations<\/strong> tab should look similar to the following figure. In this case, only the <code>$project<\/code> stage is expanded.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2644\" height=\"1326\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12.png\" alt=\"\" class=\"wp-image-105818\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12.png 2644w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12-300x150.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12-1024x514.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12-768x385.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12-1536x770.png 1536w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-12-2048x1027.png 2048w\" sizes=\"auto, (max-width: 2644px) 100vw, 2644px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>After you define your aggregation, click the <strong>Run<\/strong> button in the upper right corner to execute your query. The query should return only two documents, each one showing the total number of candidates in that department.<\/p>\n\n\n\n<p>To export your results, click the <strong>Export<\/strong> button in the upper right corner. This launches the <strong>Export<\/strong> dialog box. The dialog box displays the aggregation and lets you choose between the JSON and CSV formats, as you\u2019ve seen in the previous examples. The following figure shows the <strong>Export<\/strong> dialog box with the <strong>JSON<\/strong> option selected.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1208\" height=\"1282\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-13.png\" alt=\"\" class=\"wp-image-105819\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-13.png 1208w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-13-283x300.png 283w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-13-965x1024.png 965w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-13-768x815.png 768w\" sizes=\"auto, (max-width: 1208px) 100vw, 1208px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As with the previous examples, we\u2019ll stick with the JSON format, which is selected by default. To export the data, click the <strong>Export<\/strong> button.<\/p>\n\n\n\n<p>When the file manager window appears, navigate to the folder where you want to save the file, type a file name (or use the default), and click <strong>Select<\/strong> to generate the export file. The following figure shows the documents as they appear in the new file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"346\" height=\"346\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-14.png\" alt=\"\" class=\"wp-image-105820\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-14.png 346w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-14-300x300.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/02\/word-image-105804-14-150x150.png 150w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As expected, the file contains two documents. Each document shows the name of the department and the number of candidates in that department.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-getting-started-with-importing-and-exporting-data-in-mongodb-compass\">Getting started with importing and exporting data in MongoDB Compass<\/h2>\n\n\n\n<p>As this article has demonstrated, MongoDB Compass makes it relatively easy to import and export data. The key to importing data is to make sure that your JSON or CSV files are in the correct format. The key to exporting data is to determine whether you want to export the entire collection, a subset of the documents, or only aggregated data. You can find more details about importing and exporting data in the MongoDB topic <a href=\"https:\/\/www.mongodb.com\/docs\/compass\/current\/import-export\/\">Import and Export Data<\/a>. In the meantime, the information we covered here should give you a good sense of what it takes to import and export document data in MongoDB Compass.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with MongoDB, you\u2019ll likely need to import or export document data from time-to-time. You can use MongoDB Compass for both types of operations, working with either the JavaScript Object Notation (JSON) format or the comma-separated values (CSV) format. Both formats are commonly used to exchange data across different types of systems and applications&#8230;.&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":105821,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[53,159161],"tags":[5618,159226],"coauthors":[6779],"class_list":["post-105804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-mongodb","tag-mongodb","tag-mongodbseriesrobertsheldon"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/105804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=105804"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/105804\/revisions"}],"predecessor-version":[{"id":105835,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/105804\/revisions\/105835"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/105821"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=105804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=105804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=105804"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=105804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}