{"id":102509,"date":"2024-07-10T22:48:00","date_gmt":"2024-07-10T22:48:00","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=102509"},"modified":"2024-11-14T22:03:16","modified_gmt":"2024-11-14T22:03:16","slug":"working-with-documents-in-the-mongodb-compass-gui","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/nosql\/mongodb\/working-with-documents-in-the-mongodb-compass-gui\/","title":{"rendered":"Working with Documents in the MongoDB Compass GUI"},"content":{"rendered":"<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<p>Throughout this series, I\u2019ve discussed various ways to work with the documents in a MongoDB collection. If you\u2019ve been following along, you should now have a good sense of how to perform basic create, read, update, and delete (CRUD) operations. As you might recall, much of my focus has been on how to use MongoDB Shell to carry out these operations. This has been especially true when it comes to modifying data. In this article, I focus squarely on the MongoDB Compass GUI, demonstrating features within the interface for working with the documents directly.<\/p>\n<p>Not everyone will want to use the GUI features in Compass to perform CRUD operations, preferring instead to stick with shell commands. Even so, some of you will appreciate how easy it is to use Compass to carry out these operations, especially when performing ad hoc tasks. You might bump up against limitations in the interface, or it might seem a bit clunky at times, but for the most part, you should find the operations fairly straightforward and simple to perform.<\/p>\n<p>To help you become more familiar with the Compass interface, this article demonstrates various ways to insert, update, and delete documents, while providing numerous examples along with way. As with other aspects of MongoDB, you often have multiple options for achieving the same goals, so I\u2019ve tried to provide you with a good overview of possibilities. With that in mind, let\u2019s start digging into the Compass GUI.<\/p>\n<p>Note: For the examples in this article, I used the same MongoDB Atlas and MongoDB Compass environments I used for the previous articles in this series. Refer to the first article for more specifics about setting up these environments.<\/p>\n<h2>Inserting documents in a MongoDB collection<\/h2>\n<p>The Compass GUI makes it fairly easy to add individual documents to a collection, as you\u2019ll see in the examples to follow. For these examples, I used the <code>hr<\/code> database and <code>employees<\/code> collection. You can use any test database or collection you want, as long as the collection is empty. You might stick with the <code>hr<\/code> database and <code>employees<\/code> collection just to keep things simple.<\/p>\n<p>If you worked through the examples in previous articles, you might already have the created the <code>hr<\/code> database and <code>employees<\/code> collection, and the collection might already contain documents. To use the database and collection for this article, you should launch MongoDB Compass, establish your connection to MongoDB Atlas, and then take one of the following steps:<\/p>\n<ul>\n<li>If the <code>hr<\/code> database and <code>employees<\/code> collection exist and the collection is empty, you don\u2019t need to do anything else. You\u2019re ready to try out the examples.<\/li>\n<li>If the <code>hr<\/code> database and <code>employees<\/code> collection exist, but the collection contains documents, you should delete them. To delete the documents, open the collection, click the <strong>Delete<\/strong> button on the <strong>Documents<\/strong> tab of the main window, and then click the <strong>Delete <em>x<\/em> documents<\/strong> button in the <strong>Delete <em>x<\/em> documents<\/strong> dialog box, where <em>x<\/em> is the number of documents to be deleted. You\u2019ll then need to confirm the deletion.<\/li>\n<li>If the <code>hr<\/code> database exists but does not include the <code>employees<\/code> collection, click the <strong>Create<\/strong> <strong>collection<\/strong> button (plus sign) to the right of the <strong>hr<\/strong> database node in the left panel. In the <strong>Create<\/strong> <strong>Collection<\/strong> dialog box, type <strong>employees<\/strong> in the <strong>Collection<\/strong> <strong>Name<\/strong> text box and click <strong>Create<\/strong> <strong>Collection<\/strong>.<\/li>\n<li>If the <code>hr<\/code> database and <code>employees<\/code> collection do not exist, click the <strong>Create database<\/strong> button (plus sign) to the right of the <strong>Databases<\/strong> node in the left panel. In the <strong>Create<\/strong> <strong>Database<\/strong> dialog box, type <strong>hr<\/strong> in the <strong>Database<\/strong> <strong>Name<\/strong> text box, type <strong>employees<\/strong> in the <strong>Collection<\/strong> <strong>Name<\/strong> text box, and then click <strong>Create<\/strong> <strong>Database<\/strong>.<\/li>\n<\/ul>\n<p>Once you have your database and collection in place, you can start adding documents to the collection. The Compass GUI offers several methods for adding documents, all of which rely on the <strong>Insert<\/strong> <strong>Document<\/strong> dialog box, as described in the following sections.<\/p>\n<h3>Adding individual documents to a collection<\/h3>\n<p>click the <strong>employees<\/strong> collection node in the left panel to ensure that it is open and is the active collection. On the <strong>Documents<\/strong> tab of the main window, click the <strong>Add Data<\/strong> button and then click the <strong>Insert document<\/strong> option in the popup menu. When the <strong>Insert Document<\/strong> dialog box appears, delete the existing text and paste in the following document code:<\/p>\n<pre class=\"lang:none theme:none\">{ \n  \"_id\": 101, \n  \"name\": \"Drew\", \n  \"location\": \"Seattle\",\n  \"division\": \"Western\",\n  \"current_emp\": true,\n  \"yrs_exp\": 18,\n  \"skills\": [ \"Csharp\", \"SQL\", \"Swift\", \"C\" ],\n  \"position\": \"Senior Developer\" \n}<\/pre>\n<p>After you add your code, the <strong>Insert Document<\/strong> dialog box should look similar to the one shown in the following figure. If there are any problems with the code\u2019s syntax, the dialog box will display a message stating that an insert is not permitted while the document contains errors.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1204\" height=\"1060\" class=\"wp-image-102510\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-1.png\" \/><\/p>\n<p>Notice that the dialog box displays the name of the database and collection near the top. In this case, the dialog box shows <strong>hr.employees<\/strong>, but it will list the name of whichever database and collection you\u2019re using.<\/p>\n<p>Once you\u2019re satisfied with your document code, click the <strong>Insert<\/strong> button to add the document to the collection. The <strong>Documents<\/strong> tab of the main Compass window should now display the newly added document, as shown in the following figure. In this case, I expanded the <code>skills<\/code> array to display the individual values.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1914\" height=\"958\" class=\"wp-image-102511\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-2.png\" \/><\/p>\n<p>The example above added only a single document to the collection, but you can take a similar approach to add multiple documents. The key is to enclose the document definitions in square brackets and separate them with a comma to create an array of documents. For example, the following code adds two documents, both enclosed in a set of square brackets and separated with a comma:<\/p>\n<pre class=\"lang:none theme:none\">[\n  { \"_id\": 102, \n    \"name\": \"Parker\",\n    \"location\": \"Portland\",\n    \"division\": \"Western\",\n    \"current_emp\": true,\n    \"yrs_exp\": 14,\n    \"position\": \"Data Scientist\" },\n  { \"_id\": 103, \n    \"name\": \"Harper\", \n    \"location\": \"Chicago\",\n    \"division\": \"Western\",\n    \"current_emp\": true,\n    \"yrs_exp\": 22,\n    \"position\": \"Marketing Manager\" }\n]<\/pre>\n<p>The code defines a document with an <code>_id<\/code> value of <code>102<\/code> and one with an <code>_id<\/code> value of <code>103<\/code>. To insert these documents into the <code>employees<\/code> collection, launch the <strong>Insert<\/strong> <strong>Document<\/strong> dialog box as before, delete the existing text, paste in the above code, and then click the <strong>Insert<\/strong> button. MongoDB will add both documents to the collection, just like it added one document. The collection should now contain three documents, as shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1890\" height=\"1158\" class=\"wp-image-102512\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-3.png\" \/><\/p>\n<h3>Using the Field-by-Field Editor to add documents<\/h3>\n<p>The <strong>Insert<\/strong> <strong>Document<\/strong> dialog box also provides a more interactive way to insert a single document to a collection. This feature, referred to as the Field-by-Field Editor, lets you add the fields and their values one at a time through an interactive grid. The functionality is still a little rough around the edges, and it\u2019s a fairly time-consuming process compared to other approaches, but it could still be useful for some users.<\/p>\n<p>Let me demonstrate how it works. As in the previous examples, we\u2019ll start by launching the <strong>Insert<\/strong> <strong>Document<\/strong> dialog box and deleting the current text. Next, paste in the following code snippet:<\/p>\n<pre class=\"lang:none theme:none\">{\n  \"_id\": 104\n}<\/pre>\n<p>At this point, your document should contain only the <code>_id<\/code> field, with its value set to <code>104<\/code>. There is a reason we\u2019re taking this approach, which I\u2019ll explain shortly, but first I want to point out something else about the <strong>Insert<\/strong> <strong>Document<\/strong> dialog box. You might have noticed that, in the upper right corner, there is a <strong>View<\/strong> setting that offers two options. The options are shown in the following figure, outlined in red. The first option takes you to as JSON Mode, which is the default view and the one we\u2019ve been working in.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1204\" height=\"692\" class=\"wp-image-102513\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-4.png\" \/><\/p>\n<p>The other option (the right button) takes you to as the Field-by-Field Editor, where you can add fields interactively, as shown in the following figure. Notice that the right option is now selected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1230\" height=\"716\" class=\"wp-image-102514\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-5.png\" \/><\/p>\n<p>In this case, the <code>_id<\/code> field already exists because you defined it in JSON Mode. Had you not taken this approach, Compass would have automatically added the field and assigned a GUID as its value.<\/p>\n<p>MongoDB documentation suggests that you can change the value of the <code>_id<\/code> field in the Field-by-Field Editor, but I could not. I tried changing the field\u2019s data type to <code>String<\/code>, as the documentation suggested, but that did not help. I also tried changing the data type to <code>Int32<\/code>, but that locked up the dialog box and my only option was to cancel out. I tried to find more information about this behavior, but could not. Instead, I was also left with the impression that few people use the Field-by-Field Editor.<\/p>\n<p>Despite this issue, I still want to give you a sense of how to use the Field-by-Field Editor in case you decide to give it a go. As can see in the previous figure, when you point to a field, the <strong>Add field<\/strong> option appears (the plus sign to the left of the field name).<\/p>\n<p>To add a field, click the plus sign, and then click the <strong>Add field after _id<\/strong> option in the popup menu. This will add a row beneath the <code>_id<\/code> field, where you can type a field name and field value. You can also select a data type from the drop-down list at the right of the row.<\/p>\n<p>To define the field, type <strong>name<\/strong> for the field name and <strong>Darcy<\/strong> for the field value. The <strong>String<\/strong> data type will automatically be selected from the drop-down list. Your document should now look like the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1212\" height=\"720\" class=\"wp-image-102515\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-6.png\" \/><\/p>\n<p>If you had wanted to assign a different data type to the field, you could have selected it from the drop-down list. However, if the value is not permitted for the selected data type, the dialog box will display a message indicating an error. If this occurs, you might have to cancel what you\u2019ve done and start over.<\/p>\n<p>Once you get the hang of how to add fields, you can continue on to the next ones. In this case, you should add the following fields and their values, using the designated data types:<\/p>\n<ul>\n<li>Field: <code>location<\/code>; value: <code>Portland<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>division<\/code>; value: <code>Western<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>current_emp<\/code>; value: <code>false<\/code>; data type: <code>Boolean<\/code><\/li>\n<li>Field: <code>yrs_exp<\/code>; value: <code>6<\/code>; data type: <code>Int32<\/code><\/li>\n<li>Field: <code>position<\/code>; value: <code>Developer<\/code>; data type: <code>String<\/code><\/li>\n<\/ul>\n<p>When you\u2019re finished, your document should look like the following figure, as it appears in the Field-by-Field Editor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1210\" height=\"902\" class=\"wp-image-102516\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-7.png\" \/><\/p>\n<p>I had considered adding an array to this document, but the Field-by-Field Editor does not handle arrays or embedded documents very gracefully. You can create the field itself and assign the <code>Array<\/code> or <code>Object<\/code> data type to the field, but you cannot add the initial value without switching back to JSON Mode. I opted to wait until after I created the document to add the array, as you\u2019ll see later in the article.<\/p>\n<h3>Adding documents based on existing documents<\/h3>\n<p>Another method you can use to add a document to a collection is to copy an existing one, paste it into the <strong>Insert<\/strong> <strong>Document<\/strong> dialog box, and edit it accordingly. After you paste in the document, you can update the individual fields, without worrying about field names or data types. This can be useful if you want to quickly create a document whose schema is similar to the existing one.<\/p>\n<p>For instance, supposed you want to create a document similar to the one with an <code>_id<\/code> value of <code>104<\/code>. To copy that document, point to the document in the main Compass window. This will cause a set of icons to appear in the top right corner of the document section, providing options for editing, copying or deleting the document.<\/p>\n<p>Click the <strong>Copy to clipboard<\/strong> icon to add the document code to your clipboard. Next, click the <strong>Add Data<\/strong> button near the top of the <strong>Documents<\/strong> tab, and then click the <strong>Insert document<\/strong> option in the popup menu. In the <strong>Insert Document<\/strong> dialog box, delete the existing text and paste in the document code from the clipboard. You can then use the following field values to edit the text:<\/p>\n<ul>\n<li>Field: <code>_id<\/code>; value: <code>105<\/code>; data type: <code>Int32<\/code><\/li>\n<li>Field: <code>name<\/code>; value: <code>Carey<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>location<\/code>; value: <code>Seattle<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>division<\/code>; value: <code>Western<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>current_emp<\/code>; value: <code>true<\/code>; data type: <code>Boolean<\/code><\/li>\n<li>Field: <code>yrs_exp<\/code>; value: <code>7<\/code>; data type: <code>Int32<\/code><\/li>\n<li>Field: <code>position<\/code>; value: <code>SEO Specialist<\/code>; data type: <code>String<\/code><\/li>\n<\/ul>\n<p>When you\u2019re finished, your document should look like the one shown in the following code:<\/p>\n<pre class=\"lang:none theme:none\">{\n  \"_id\": 105,\n  \"name\": \"Carey\", \n  \"location\": \"Seattle\",\n  \"division\": \"Western\",\n  \"current_emp\": true,\n  \"yrs_exp\": 7,\n  \"position\": \"SEO Specialist\"\n}<\/pre>\n<p>Once you\u2019re satisfied with your changes, click the <strong>Insert<\/strong> button to add the document to the <code>employees<\/code> collection.<\/p>\n<p>In addition to the <strong>Copy to clipboard<\/strong> icon, the document section also includes the <strong>Clone document<\/strong> icon. This one is handy because, when you click it, Compass automatically launches the <strong>Insert Document<\/strong> dialog box and pastes in the document\u2019s code. The only catch is that it does not include the <code>_id<\/code> field. You must provide the field yourself, unless you want MongoDB to automatically generate a GUID for that field\u2019s value when the document is added to the collection.<\/p>\n<p>You can test out the cloning feature with the document you just added in the previous example. Point to the document in the main Compass window, and then click the <strong>Clone document<\/strong> icon. In the <strong>Insert Document<\/strong> dialog box, add the following code snippet above the <code>name<\/code> field (including the comma):<\/p>\n<pre class=\"lang:none theme:none\">\"_id\": 106,<\/pre>\n<p>Next, use the following information to update the other field values:<\/p>\n<ul>\n<li>Field: <code>name<\/code>; value: <code>Avery<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>location<\/code>; value: <code>Chicago<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>division<\/code>; value: <code>Western<\/code>; data type: <code>String<\/code><\/li>\n<li>Field: <code>current_emp<\/code>; value: <code>true<\/code>; data type: <code>Boolean<\/code><\/li>\n<li>Field: <code>yrs_exp<\/code>; value: <code>11<\/code>; data type: <code>Int32<\/code><\/li>\n<li>Field: <code>position<\/code>; value: <code>Network Admin<\/code>; data type: <code>String<\/code><\/li>\n<\/ul>\n<p>When you\u2019re finished, your document should look like the one shown in the following code:<\/p>\n<pre class=\"lang:none theme:none\">{\n  \"_id\": 106,\n  \"name\": \"Avery\", \n  \"location\": \"Chicago\",\n  \"division\": \"Western\",\n  \"current_emp\": true,\n  \"yrs_exp\": 11,\n  \"position\": \"Network Admin\"\n}<\/pre>\n<p>After you\u2019re done editing the document, click the <strong>Insert<\/strong> button to add it to the <code>employees<\/code> collection. You should now have six documents in your sample collection\u2014more than enough to demonstrate how to update documents within the Compass GUI.<\/p>\n<h2>Updating MongoDB documents individually<\/h2>\n<p>The Compass GUI provides two methods for modifying documents in a MongoDB collection: updating the documents individually or updating them in bulk. To update a document individually, you edit the document directly in the interface, which you can do in List View, JSON View, or Table View.<\/p>\n<h3>Updating documents in List View<\/h3>\n<p>To update an individual document in List View, start by pointing to any field within the document. As you saw earlier, this will cause a set of icons to appear in the upper right corner of the document section, as shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1758\" height=\"1148\" class=\"wp-image-102517\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-8.png\" \/><\/p>\n<p>In this case, the document with an <code>_id<\/code> value of <code>102<\/code> is currently selected. To update the document, click the <strong>Edit document<\/strong> icon (the pencil). This places the document in editing mode, where you can modify individual field names, field values, or data types. For example, you can change the <code>yrs_exp<\/code> value from <code>14<\/code> to <code>12<\/code>, as shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1736\" height=\"458\" class=\"wp-image-102518\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-9.png\" \/><\/p>\n<p>After you modify the data, click the <strong>Update<\/strong> button to save your edits. This will persist your changes to the database.<\/p>\n<p>You can also update a field name in the same way. For instance, you might want to change the name of the <code>yrs_exp<\/code> field to <code>years_exp<\/code>. Once again, go into editing mode, make your change, and click <strong>Update<\/strong>.<\/p>\n<p>Another option for updating a document is to add a field. For instance, suppose you want to add the <code>hire_date<\/code> field to the document with an <code>_id<\/code> value of <code>103<\/code>. As before, start by putting the document into editing mode. Next, point to the <strong>yrs_exp<\/strong> field listing, click the plus sign to the left of the field, and then click the <strong>Add field after yrs_exp<\/strong> option in the popup menu.<\/p>\n<p>This will add a row beneath the <code>yrs_exp<\/code> field where you can enter the necessary information for the new field:<\/p>\n<ul>\n<li>For the field name, type <strong>hire_date<\/strong>.<\/li>\n<li>For the field value, type <strong>May 13, 2016<\/strong>.<\/li>\n<li>For the data type, select <strong>Date<\/strong> from the drop-down list.<\/li>\n<\/ul>\n<p>When you\u2019re finished, click <strong>Update<\/strong>. The document should now include the <code>hire_date<\/code> field.<\/p>\n<p>You can also remove fields from a document. For example, you might decide to delete the <code>current_emp<\/code> field. As before, put the document in editing mode. Next, point to the field listing, click the <strong>Remove field<\/strong> icon (trash can) to the left of the field name, and then click <strong>Update<\/strong>. Your document should now look similar to the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1724\" height=\"374\" class=\"wp-image-102519\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-10.png\" \/><\/p>\n<p>Notice that the document includes the <code>hire_date<\/code> field but no longer includes the <code>current_emp<\/code> field. Also notice that the <code>hire_date<\/code> field is listed at the end of the document, even though you added it after the <code>yrs_exp<\/code> field. MongoDB will typically add new fields to the end of a document when using List View. It will also usually move a field to the end if you change the field name.<\/p>\n<h3>Updating documents in JSON View<\/h3>\n<p>You can also update documents directly in JSON View, although the process is a little different from List View. You still put the document into editing mode, but you don\u2019t have the types of forms or text boxes you get in the other views. Instead, you work with the code directly, just like you would in a text editor.<\/p>\n<p>For example, you might want to make two changes to the document with an <code>_id<\/code> value of <code>104<\/code>. The first change is to update the <code>current_emp<\/code> value to <code>true<\/code>, and the second change is to add the <code>skills<\/code> array to the document.<\/p>\n<p>To make the first change, enter editing mode and modify the text directly, simply changing <code>false<\/code> to <code>true<\/code>. To make the second change, add a line to the code beneath the <code>yrs_exp<\/code> field, and then insert the following field definition (including the comma):<\/p>\n<pre class=\"lang:none theme:none\">\"skills\": [ \"Java\", \"SQL\", \"Python\", \"PHP\" ],<\/pre>\n<p>Your document should now include both edits, as shown in the following figure, although your changes are not committed to the database as long as you remain in editing mode.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1726\" height=\"500\" class=\"wp-image-102520\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-11.png\" \/><\/p>\n<p>Once you\u2019re satisfied with your updates, click the <strong>Replace<\/strong> button. The document should now look similar to this next figure, as it appears in JSON View. In this case, I\u2019ve expanded the <code>skills<\/code> array to display all its values.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1738\" height=\"576\" class=\"wp-image-102521\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-12.png\" \/><\/p>\n<p>Notice that the <code>skills<\/code> array was not added at the end of the document but rather where you inserted it, after the <code>yrs_exp<\/code> field. This is because Compass replaces the document when you\u2019re in JSON View, rather than updating the document, like you saw in List View.<\/p>\n<p>Now let\u2019s modify the document that has an <code>_id<\/code> value of <code>105<\/code>. For this document, you\u2019ll delete the <code>yrs_exp<\/code> and <code>position<\/code> fields and replace them with the following code, which creates the <code>position<\/code> embedded document:<\/p>\n<pre class=\"lang:none theme:none\">\"position\": { \n  \"title\": \"SEO Specialist\", \n  \"dept\": \"Marketing\",\n  \"yrs_exp\": 7 }<\/pre>\n<p>The <code>position<\/code> field includes the <code>yrs_exp<\/code> embedded field, which is why you deleted the original field. After you update the code, click <strong>Replace<\/strong>. Your document should now look similar to the one in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1740\" height=\"492\" class=\"wp-image-102522\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-13.png\" \/><\/p>\n<p>The more comfortable you become with building documents in MongoDB, the greater you\u2019ll appreciate how quickly and easily you can update documents in JSON view, but you must understand how to build a document definition.<\/p>\n<h3>Updating documents in Table View<\/h3>\n<p>As with List View and JSON View, you can also update documents directly in Table View by putting the document into editing mode, like you\u2019ve been doing. For example, you might decide to add the <code>current_emp<\/code> field back into the document with an <code>_id<\/code> value of <code>103<\/code>, only this time, setting its value to <code>true<\/code>, rather than <code>false<\/code>.<\/p>\n<p>To add the field, go into editing mode for that document, click the <code>current_emp<\/code> field, and change its value to <code>true<\/code>, as shown in the following figure. After you\u2019ve modified the data, click the <strong>Update<\/strong> button to save your changes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2480\" height=\"748\" class=\"wp-image-102523\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-14.png\" \/><\/p>\n<p>In this case, it was particularly easy to add the field to the document because the field already existed in other documents, so there was already a space for it in the Table View grid.<\/p>\n<p>However, if the field you want to add does not exist in other documents, you must specifically add the field to the grid. To do so, click the plus sign associated with one of the fields and then click the <strong>Add field after <em>fieldname<\/em><\/strong> option in the popup menu, where <strong><em>fieldname<\/em><\/strong> is the name of the current field. This will add a column at the right end of the grid, where you can enter the new field information.<\/p>\n<h2>Updating MongoDB documents in bulk<\/h2>\n<p>You\u2019ll inevitably run into a situation where you\u2019ll want to update multiple documents at the same time. For example, you might be asked to change the <code>division<\/code> value in each document in the <code>employees<\/code> collection from <code>Western<\/code> to <code>North<\/code> <code>America<\/code>. Currently, the collection contains only six documents, but a collection could easily include thousands of documents.<\/p>\n<p>To update the <code>employees<\/code> collection, click the <strong>Update<\/strong> button in the main Compass window. In the <strong>Update 6 documents<\/strong> dialog box, add the following code snippet to the third line:<\/p>\n<pre class=\"lang:none theme:none\">\"division\": \"North America\"<\/pre>\n<p>The code indicates that the <code>division<\/code> value in each document should be changed to <code>North<\/code> <code>America<\/code>. After you update the document, the dialog box should look similar to the one shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1224\" height=\"1184\" class=\"wp-image-102524\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-15.png\" \/><\/p>\n<p>Next, click the <strong>Update 6 documents<\/strong> button. MongoDB will update the six documents with the new value. However, you will not see your changes until you refresh the main window.<\/p>\n<p>At times, you might need to update only a subset of the documents in the collection. To do a partial update, you must first filter the documents so you end up with a subset that includes only the documents to be modified.<\/p>\n<p>For example, you might want to update the documents with a <code>location<\/code> value of <code>Chicago<\/code>, changing that value to <code>Minneapolis<\/code>. To modify the documents, first create a search filter that returns only the documents with a <code>location<\/code> value of <code>Chicago<\/code>. This requires that you type or paste the following code snippet into the <strong>Filter<\/strong> text box on the query bar:<\/p>\n<pre class=\"lang:none theme:none\">{\"location\": \"Chicago\"}<\/pre>\n<p>In some cases, the <strong>Filter<\/strong> text box will automatically add the curly brackets, depending on how you enter the code. It might also remove duplicate brackets or just leave them. Again, it depends on how you enter the text. No matter what the behavior, however, you should end up with exactly one set of curly brackets surrounding the code.<\/p>\n<p>Once the query is defined, click the <strong>Find<\/strong> button to filter the documents. The main Compass window should now display only two documents, each one with a <code>location<\/code> value of <code>Chicago<\/code>, as shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1938\" height=\"1070\" class=\"wp-image-102525\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-16.png\" \/><\/p>\n<p>After you filter the documents, click the <strong>Update<\/strong> button. In the <strong>Update 2 documents<\/strong> dialog box, add the following code to the third line:<\/p>\n<pre class=\"lang:none theme:none\">\"location\": \"Minneapolis\"<\/pre>\n<p>The dialog box should now look similar to the one shown in the following figure. Notice that the filter you defined in the previous step is included in the <strong>Filter<\/strong> text box.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1218\" height=\"1180\" class=\"wp-image-102526\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-17.png\" \/><\/p>\n<p>Once you\u2019re satisfied with your code, click the <strong>Update 2 documents<\/strong> button. MongoDB will update the two documents. However, you\u2019ll need to reset the query filter before you can see your changes. Otherwise, Compass will display only those documents with a <code>location<\/code> value of <code>Chicago<\/code>, of which there are now none.<\/p>\n<h2>Deleting documents in a MongoDB collection<\/h2>\n<p>As with updating documents, Compass lets you delete documents individually or in bulk. To delete an individual document, point to the document in List View, JSON View, or Table View. When the document\u2019s set of icons appear, click the <strong>Remove document<\/strong> icon (trash can). You\u2019ll then be presented with the option to delete the document.<\/p>\n<p>For example, to remove the document with an <code>_id<\/code> value of <code>104<\/code>, point to the document in List View (or one of the other views) and click the <strong>Remove document<\/strong> icon. The document will then look like the one in the following figure. Click <strong>Delete<\/strong> to remove the document.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1910\" height=\"494\" class=\"wp-image-102527\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-18.png\" \/><\/p>\n<p>In some cases, you might want to delete multiple documents at that same time. For this, you can take the same approach you did when updating documents in bulk. First, create a query that filters out all documents except those you want to delete, and then delete those specific documents.<\/p>\n<p>For example, to delete all documents with a <code>location<\/code> value of <code>Minneapolis<\/code>, start by entering the following code snippet into the <strong>Filter<\/strong> text box in the query bar:<\/p>\n<pre class=\"lang:none theme:none\">\"location\": \"Minneapolis\"<\/pre>\n<p>After you\u2019ve defined your query, click the <strong>Find<\/strong> button to filter the documents. The main Compass window should now display only two documents, each one with a <code>location<\/code> value of <code>Minneapolis<\/code>.<\/p>\n<p>Next, click the <strong>Delete<\/strong> button to launch the <strong>Delete 2 documents<\/strong> dialog box, shown in the following figure. Once again, the <strong>Filter<\/strong> text box includes the filter you defined in the previous step.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1224\" height=\"1282\" class=\"wp-image-102528\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-19.png\" \/><\/p>\n<p>To remove the documents, click the <strong>Delete 2 documents<\/strong> button. When Compass prompts you to confirm your deletion, click the <strong>Delete 2 documents<\/strong> button again. Be sure to reset the query filter to display the remaining documents.<\/p>\n<p>In some cases, you might want to delete all documents in a collection, which can be especially useful in a development or test environment. For example, to delete the remaining documents in the <code>employees<\/code> collection, click the <strong>Delete<\/strong> button near the top of the tab to open the <strong>Delete 3 documents<\/strong> dialog box, shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1216\" height=\"1282\" class=\"wp-image-102529\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2024\/05\/word-image-102509-20.png\" \/><\/p>\n<p>Click the <strong>Delete 3 documents<\/strong> button, and then click the button again when prompted to confirm your deletion. Remember to refresh the main document window to verify that all the documents have been removed. The collection should now be empty.<\/p>\n<h2>Getting started with the MongoDB Compass GUI<\/h2>\n<p>MongoDB Compass makes it fairly painless to add documents to a collection, as well as to update or delete them. You can also work with documents individually or in bulk. The ability to carry out these tasks can be useful for anyone who needs to manipulate content on an ad hoc basis. For example, developers might use Compass to quickly update their test data while trying out features in their applications, or QA personnel might use Compass to add documents to a collection to test certain functionality. Of course, some users will still prefer to use the command line to carry out tasks, but for those who want a quick and easy way to manage their MongoDB documents, Compass can be a good place to start.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throughout this series, I\u2019ve discussed various ways to work with the documents in a MongoDB collection. If you\u2019ve been following along, you should now have a good sense of how to perform basic create, read, update, and delete (CRUD) operations. As you might recall, much of my focus has been on how to use MongoDB&#8230;&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":103263,"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-102509","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\/102509","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=102509"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/102509\/revisions"}],"predecessor-version":[{"id":103071,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/102509\/revisions\/103071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/103263"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=102509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=102509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=102509"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=102509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}