Modify Documents修改文档
You can edit existing documents in your collection.您可以编辑集合中的现有文档。
When you edit a document in List or Table view, Compass performs a findOneAndUpdate
operation and updates only those fields that you have changed.当您在“列表”或“表”视图中编辑文档时,Compass会执行findOneAndUpdate
操作,并仅更新那些已更改的字段。
When you edit a document in JSON view, Compass performs a findOneAndReplace
operation and replaces the document.在JSON视图中编辑文档时,Compass会执行findOneAndReplace
操作并替换文档。
Limitations局限性
Modifying documents is not permitted in MongoDB Compass Readonly Edition.MongoDB Compass只读版不允许修改文档。
Procedure过程
Select the appropriate tab based on whether you are viewing your documents in List, JSON, or Table view:根据您是在列表、JSON还是表视图中查看文档,选择适当的选项卡:

To modify a document, hover over the document and click the pencil icon:要修改文档,请将鼠标悬停在文档上,然后单击铅笔图标:

After you click the pencil icon, the document enters edit mode:单击铅笔图标后,文档将进入编辑模式:

You can now make changes to the fields, values, or data types of values.现在,您可以更改字段、值或值的数据类型。
Delete Fields删除字段
To delete a field from a document, click the icon to the left of the field:要从文档中删除字段,请单击字段左侧的+图标:

Once selected, the field is marked for removal and appears highlighted in red:选择后,该字段将被标记为删除,并以红色突出显示:

Add New Fields添加新字段
To add a new field in the document after an existing field, hover over the row number in the dialog and click on the plus sign. 要在文档中的现有字段后添加新字段,请将鼠标悬停在对话框中的行号上,然后单击加号。The row number is not part of the document but is part of the dialog display.行号不是文档的一部分,而是对话框显示的一部分。
You can also add a new field at the end of the document by pressing the tab key when your text cursor is in the value of the last document field.当文本游标位于最后一个文档字段的值时,也可以通过按tab键在文档末尾添加新字段。
Modify an Existing Field修改现有字段
To modify documents, click on existing field names or values and make changes. 若要修改文档,请单击现有字段名称或值并进行更改。In this example, the airline was changed from 在本例中,航空公司从4
to 2
. 4
更改为2
。Changed fields appear highlighted in yellow:更改的字段以黄色突出显示:

When you edit a document in List or Table view, Compass performs a findOneAndUpdate
operation and updates only those fields that you have changed.当您在“列表”或“表”视图中编辑文档时,Compass会执行findOneAndUpdate
操作,并仅更新那些已更改的字段。
If Compass detects that you have changed fields that were modified outside of Compass, it notifies you, preventing you from accidentally overwriting the changes made outside of Compass. 如果Compass检测到您更改了在Compass之外修改的字段,它会通知您,防止您意外覆盖在Compass之外所做的更改。You can choose to proceed and replace the document by clicking Update, or cancel your changes.您可以选择继续并通过单击“更新”替换文档,或者取消更改。
Save Changes保存更改
When you are finished editing the document, click the 编辑完文档后,单击“更新”按钮提交更改。Update
button to commit your changes.
Revert a Change还原更改
To revert changes to a document, hover over the edited field and click the revert icon which appears to the left of the field's line number.要恢复对文档的更改,请将鼠标悬停在编辑过的字段上,然后单击显示在字段行号左侧的“恢复图标”。

JSON View is available starting in Compass 1.20.JSON视图从Compass 1.20开始提供。
To modify a document, hover over the document and click the pencil icon:要修改文档,请将鼠标悬停在文档上,然后单击铅笔图标:

After you click the pencil icon, the document enters edit mode. You can now add, remove, and edit field values by modifying the JSON document.单击铅笔图标后,文档将进入编辑模式。现在,您可以通过修改JSON文档来添加、删除和编辑字段值。
By default, this view hides embedded objects and arrays. To expand embedded objects and array elements, hover over the target document and click the top arrow on the left side of the document.默认情况下,此视图会隐藏嵌入的对象和数组。若要展开嵌入的对象和数组元素,请将鼠标悬停在目标文档上,然后单击文档左侧的顶部箭头。
To expand individual objects and arrays, click the arrow to the left of the desired field.要展开各个对象和数组,请单击所需字段左侧的箭头。

When you edit a document in JSON view, Compass performs a 在JSON视图中编辑文档时,Compass会执行findOneAndReplace
operation and replaces the document.findOneAndReplace
操作并替换文档。
If Compass detects that you have changed fields that were modified outside of Compass, it notifies you, preventing you from accidentally overwriting the changes made outside of Compass. You can choose to proceed and replace the document by clicking Update, or cancel your changes.如果Compass检测到您更改了在Compass之外修改的字段,它会通知您,防止您意外覆盖在Compass之外所做的更改。您可以选择继续并通过单击“更新”替换文档,或者取消更改。
To modify a document, hover over the document and click the pencil icon:要修改文档,请将鼠标悬停在文档上,然后单击铅笔图标:

After you click the pencil icon, the document enters edit mode:单击铅笔图标后,文档将进入编辑模式:

When you edit a document in List or Table view, Compass performs a 当您在“列表”或“表格”视图中编辑文档时,Compass会执行findOneAndUpdate
operation and updates only those fields that you have changed.findOneAndUpdate
操作,并仅更新那些已更改的字段。
If Compass detects that you have changed fields that were modified outside of Compass, it notifies you, preventing you from accidentally overwriting the changes made outside of Compass. 如果Compass检测到您更改了在Compass之外修改的字段,它会通知您,防止您意外覆盖在Compass之外所做的更改。You can choose to proceed and replace the document by clicking Update, or cancel your changes.您可以选择继续并通过单击“更新”替换文档,或者取消更改。
Delete Fields删除字段
To delete a field from a document:要从文档中删除字段,请执行以下操作:
-
Click the value of the field you want to delete.单击要删除的字段的值。 -
Click the icon.单击图标。 -
Click Update to confirm your changes.单击“更新”以确认您的更改。
Add New Fields添加新字段
To add a new field to the document:要向文档添加新字段,请执行以下操作:
-
Click the field after which you wish to add the new field.单击要添加新字段的字段。 -
Click the icon.单击图标。 -
Click Add Field after <Field Name>.单击“在<Field Name>后面添加字段”。 -
Populate your newly created field.填充新创建的字段。 -
Click Update to confirm your changes.单击“更新”以确认您的更改。
Revert a Change还原更改
While modifying a document, you have the option to revert changes made to a field prior to saving the modified document.修改文档时,您可以选择在保存修改后的文档之前恢复对字段所做的更改。
Click the revert icon which appears on the right side of the edited table element.单击显示在编辑的表格元素右侧的“还原图标”。

Cancel Changes取消更改
To exit the edit mode and cancel all pending changes to the document, click the Cancel button.要退出编辑模式并取消对文档的所有挂起的更改,请单击“取消”按钮。