Change a Document更改文档

You can change documents in a MongoDB collection using two distinct operation types: update and replace. 您可以使用两种不同的操作类型来更改MongoDB集合中的文档:updatereplaceUpdate operations mutate specified fields in one or more documents and leave other fields and values unchanged. 更新操作会改变一个或多个文档中的指定字段,并保持其他字段和值不变。Replace operations remove all existing fields in one or more documents and substitute them with specified fields and values.替换操作删除一个或多个文档中的所有现有字段,并用指定的字段和值替换它们。

To perform an update to one or more documents, create an update document that specifies the update operator (the type of update to perform) and the fields and values that describe the change. 要对一个或多个文档执行更新,请创建一个更新文档,该文档指定更新运算符(要执行的更新类型)以及描述更改的字段和值。Update documents use the following format:更新文档时使用以下格式:

{
   <update operator>: {
      <field> : {
         ...
      },
      <field> : {
      }
   },
   <update operator>: {
      ...
   }
}

The top level of an update document contains one or more of the following update operators:更新文档的顶层包含以下一个或多个更新运算符:

  • $set - replaces the value of a field with a specified one将字段的值替换为指定的值
  • $inc - increments or decrements field values增加或减少字段值
  • $rename - renames fields重命名字段
  • $unset - removes fields删除字段
  • $mul - multiplies a field value by a specified number将字段值乘以指定的数字

See the MongoDB server manual for a complete list of update operators and their usage.有关更新操作符及其用法的完整列表,请参阅MongoDB服务器手册。

The update operators apply only to the fields associated with them in your update document.更新运算符仅适用于更新文档中与其关联的字段。

Note
Aggregation Pipelines in Update Operations更新操作中的聚合管道

If you are using MongoDB Version 4.2 or later, you can use aggregation pipelines made up of a subset of aggregation stages in update operations. 如果您使用的是MongoDB 4.2版或更高版本,则可以在更新操作中使用由聚合阶段子集组成的聚合管道。For more information on the aggregation stages MongoDB supports in aggregation pipelines used in update operations, see our tutorial on building updates with aggregation pipelines.有关更新操作中使用的聚合管道中MongoDB支持的聚合阶段的更多信息,请参阅我们关于使用聚合管道构建更新的教程。

Consider a document of fields containing the English alphabet of lowercase letters a through z and their ordinal values:考虑包含小写字母az的英语字母表及其顺序值的字段的文档:

{
   _id: 465,
   a: 1,
   b: 2,
   c: 3,
   ...
   y: 25,
   z: 26,
}

If you apply the $set update operator with a specified value for z, your update operation might resemble the following:如果使用指定的z值应用$set更新运算符,则更新操作可能类似于以下内容:

const filter = { _id: 465 };
// update the value of the 'z' field to 42
const updateDocument = {
   $set: {
      z: 42,
   },
};
const result = await collection.updateOne(filter, updateDocument);

The updated document resembles the following, with an an updated value in the z field and all other values unchanged:更新后的文档类似于以下内容,z字段中有一个更新的值,所有其他值不变:

{
   _id: 465,
   a: 1,
   ...
   y: 25,
   z: 42,
}

If an update operation fails to match any documents in a collection, it does not make any changes. 如果更新操作无法匹配集合中的任何文档,则不会进行任何更改。Update operations can be configured to perform an upsert which attempts to perform an update, but if no documents are matched, inserts a new document.更新操作可以配置为执行upsert,它尝试执行更新,但如果没有匹配的文档,则插入新文档。

You cannot modify the _id field of a document nor change a field to a value that violates a unique index constraint. 不能修改文档的_id字段,也不能将字段更改为违反唯一索引约束的值。See the MongoDB server manual for more information on unique indexes.有关唯一索引的更多信息,请参阅MongoDB服务器手册。

To perform a replacement operation, create a replacement document that consists of the fields and values that you would like to insert in your replace operation. 要执行替换操作,请创建一个替换文档,该文档包含要在替换操作中插入的字段和值。Replacement documents use the following format:替换文档使用以下格式:

{
   <field>: {
      <value>
   },
   <field>: {
      ...
   }
}

Replacement documents are the documents that you want to take the place of existing documents that match the query filters.替换文档是要替换与查询筛选器匹配的现有文档的文档。

Consider a document of fields containing the English alphabet of lowercase letters a through z and their ordinal values:考虑包含小写字母az的英语字母表及其顺序值的字段的文档:

{
   _id: 465,
   a: 1,
   b: 2,
   c: 3,
   ...
   y: 25,
   z: 26,
}

Suppose you wanted to replace this document with one that contains only the field z with a value of 42. 假设您希望将此文档替换为仅包含值为42的字段z的文档。Your replacement operation might resemble the following:您的更换操作可能类似于以下操作:

const filter = { _id: 465 };
// 用替换文档替换匹配的文档
const replacementDocument = {
   z: 42,
};
const result = await collection.replaceOne(filter, replacementDocument);

The replaced document contains the contents of the replacement document and the immutable _id field as follows:替换的文档包含替换文档的内容和不可变的_id字段,如下所示:

{
   _id: 465,
   z: 42,
}

If a replace operation fails to match any documents in a collection, it does not make any changes. 如果替换操作无法匹配集合中的任何文档,则不会进行任何更改。Replace operations can be configured to perform an upsert which attempts to perform the replacement, but if no documents are matched, it inserts a new document.替换操作可以配置为执行upsert,它尝试执行替换,但如果没有匹配的文档,则会插入新文档。

You cannot modify the _id field of a document nor change a field to a value that violates a unique index constraint. 不能修改文档的_id字段,也不能将字段更改为违反唯一索引约束的值。See the MongoDB server manual for more information on unique indexes.有关唯一索引的更多信息,请参阅MongoDB服务器手册。