You can perform bulk update operations on multiple documents in Compass by using the Update Documents modal. Performing updates with the Update Documents modal helps you visualize updates to your data before you apply them.您可以使用“更新文档”模式在Compass中对多个文档执行批量更新操作。使用“更新文档”模式执行更新可以帮助您在应用数据更新之前将其可视化。
About this Task关于此任务
You can use any syntax that works with the您可以使用任何适用于updateparameter ofdb.collection.updateMany().db.collection.updateMany()的update参数的语法。The Update Documents modal does not support any“更新文档”模式不支持任何optionsparameters such as upsert, writeConcern, or collation.options参数,如upstart、writeConcern或collation。Previews of the documents affected by bulk update operations are only visible if your database is configured to support transactions. For details, see /core/transactions.只有当数据库配置为支持事务时,受批量更新操作影响的文档的预览才可见。有关详细信息,请参阅事务。
Before you Begin开始之前
The Update Documents modal is available starting in Compass version “更新文档”模式从Compass 1.42.0版本开始可用。有关更新或安装最新Compass版本的说明,请参阅下载和安装Compass。1.42.0. For instructions on updating or installing the latest Compass version, see Download and Install Compass.
Steps步骤
Apply a query filter应用查询筛选器
From the Documents tab, input a query into the Query bar. The filter criteria of the query specified applies to the documents in the Bulk Update modal. If you need to apply an update to all documents in a collection, leave the Query bar blank.从“文档”选项卡中,在查询栏中输入查询。指定查询的筛选条件适用于“批量更新”模式中的文档。如果需要对集合中的所有文档应用更新,请将“查询栏”留空。
Open the bulk update modal打开批量更新模式
On the Documents tab, click the Update button, or right-click the surrounding white space and select Bulk update from the dropdown, to display the Update Documents modal. The following table summarizes the UI of the modal:在“文档”选项卡上,单击“更新”按钮,或右键单击周围的空白并从下拉列表中选择“批量更新”,以显示“更新文档”模式。下表总结了该模式的用户界面:
| Filter(筛选) | |
| Update(更新) | update parameter of the db.collection.updateMany().db.collection.updateMany()的update参数的语法。 |
| Preview(预览) |
Enter the update syntax输入更新语法
In the Update text field, provide the update syntax.在“更新”文本字段中,提供更新语法。
The number of documents affected by the update displays at the top of the Update Documents modal.受更新影响的文档数量显示在“更新文档”模式的顶部。
Note
The documents under the Preview header show how the Update syntax affects documents in your collection.“预览”标题下的文档显示了“更新”语法如何影响集合中的文档。
(Optional) Name and save your Update(可选)命名并保存更新
You can name and save the update query in the Update Documents modal. Saving your query adds it to your favorite queries for that collection and allows you to load and copy the query after you close the modal.您可以在“更新文档”模式中命名并保存更新查询。保存查询会将其添加到该集合的常用查询中,并允许您在关闭模式后加载和复制查询。
Click the Save button on the bottom left of the modal.单击模式左下角的“保存”按钮。Enter a name for the update syntax.输入更新语法的名称。Click the green Save button.单击绿色的“保存”按钮。
Example示例
The following example uses the sample_mflix dataset.以下示例使用sample_mflix数据集。
This example updates the 此示例使用tomatoes.viewer.numReviews and tomatoes.viewer.meter fields with the Compass Update Documents modal.tomatoes.viewer.numReviews模式更新tomatoes.viewer.numReviews和tomatoes.viewer.meter字段。
Apply a filter in the Query Bar to filter movies which have a 在“查询栏”中应用筛选器以筛选year of 1917.1917年的电影。
{ 'year' : 1917 }
Click the Update button, the Update Documents modal displays.单击“更新”按钮,将显示“更新文档”模式。
In the Update text box, paste the following syntax:在“更新”文本框中,粘贴以下语法:
{
$inc: { "tomatoes.viewer.numReviews" : 1},
$set: { "tomatoes.viewer.meter" : 99 }
}
This syntax:此语法:
Increments the将tomatoes.viewer.numReviewsfield by1.tomatoes.viewer.numReviews字段增加1。Sets the将tomatoes.viewer.meterfield to99.tomatoes.viewer.meter字段设置为99。
The Preview section populates with sample documents affected by the update query.“预览”部分填充受更新查询影响的示例文档。
To view the updates to the numReviews and meter fields:要查看numReviews和meter字段的更新:
Click the arrow icon next to tomatoes.单击tomatoes旁边的箭头图标。Click the arrow icon next to viewer.单击viewer旁边的箭头图标。
Click the Update Documents button to update the collection's data.单击“更新文档”按钮以更新集合的数据。