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操作并替换文档。

  • Modifying documents is not permitted in MongoDB Compass Readonly Edition.MongoDB Compass只读版不允许修改文档。

Select the appropriate tab based on whether you are viewing your documents in List, JSON, or Table view:根据您是在列表、JSON还是在表视图中查看文档,选择适当的选项卡:

Document View Selection

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

Document Edit Selection
click to enlarge

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

Document Edit Mode
click to enlarge

You can now make changes to the fields, values, or data types of values.现在可以对字段、值或值的数据类型进行更改。

To delete a field from a document, click the x icon to the left of the field:要从文档中删除字段,请单击字段左侧的x图标:

Document Deletion

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

Document Removal View

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键在文档末尾添加新字段。

To modify documents, click on existing field names or values and make changes. 要修改文档,请单击现有字段名或值并进行更改。In this example, the flight status was changed from L to M. 在本例中,飞行状态从L更改为MChanged fields appear highlighted in yellow:更改的字段以黄色突出显示:

Document Update View
click to enlarge

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.您可以通过单击“更新”选择继续并替换文档,或取消更改。

When you are finished editing the document, click the Update button to commit your changes.编辑完文档后,单击“更新”按钮提交更改。

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.要恢复对文档的更改,请将鼠标悬停在已编辑的字段上,然后单击字段行号左侧的“恢复”图标。

Revert Document in List View
click to enlarge
Note

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:要修改文档,请将鼠标悬停在文档上,然后单击铅笔图标:

Document Edit Selection in JSON View
click to enlarge

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.要展开单个对象和阵列,请单击所需字段左侧的箭头。

Expand embedded objects in JSON view
click to enlarge

When you edit a document in JSON view, Compass performs a findOneAndReplace operation and replaces the document.在JSON视图中编辑文档时,Compass执行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. 如果Compass检测到您更改了在Compass之外修改的字段,它会通知您,防止您意外覆盖在Compass之外所做的更改。You can choose to proceed and replace the document by clicking Update, or cancel your changes.您可以通过单击“更新”选择继续并替换文档,或取消更改。

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

Document Edit Selection in Table View
click to enlarge

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

Document Edit Mode in Table View
click to enlarge

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.您可以通过单击“更新”选择继续并替换文档,或取消更改。

To delete a field from a document:要从文档中删除字段,请执行以下操作:

  1. Click the value of the field you want to delete.单击要删除的字段的值。
  2. Click the icon.单击图标。
  3. Click Update to confirm your changes.单击“更新”确认您的更改。

To add a new field to the document:要向文档中添加新字段,请执行以下操作:

  1. Click the field after which you wish to add the new field.单击要添加新字段的字段。
  2. Click the icon.单击图标。
  3. Click Add Field after <Field Name>.点击“在<Field Name>之后添加字段”
  4. Populate your newly created field.填充新创建的字段。
  5. Click Update to confirm your changes.单击“更新”确认您的更改。

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.单击显示在已编辑表格元素右侧的“还原”图标。

Document Revert Changes in Table View

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