Change a Document更改文档
On this page本页内容
Overview概述
You can change documents in a MongoDB collection using either update or replace operations. 您可以使用更新或替换操作更改MongoDB集合中的文档。Update 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.替换操作将删除一个或多个文档中的所有现有字段,并用指定的字段和值替换它们。
Update更新
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 Server手册。
The update operators apply only to the fields associated with them in your update document.更新运算符仅应用于更新文档中与其关联的字段。
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在更新操作中使用的聚合管道中支持的聚合阶段的更多信息,请参阅关于使用聚合管道构建更新的教程。
Example实例
Consider a document in the 考虑myDB.items
collection with fields describing an item for sale, its price, and the quantity available:myDB.items
集合中的一个文档,该文档包含描述待售物品、价格和可用数量的字段:
{
_id: 465,
item: "Hand-thrown ceramic plate",
price: 32.50,
quantity: 7,
}
If you apply the 如果使用新的$set
update operator with a new value for quantity
, you can use the following update document:quantity
值应用$set
更新运算符,则可以使用以下更新文档:
const myDB = client.db("myDB");
const myColl = myDB.collection("items");
const filter = { _id: 465 };
//update the value of the 'quantity' field to 5将“数量”字段的值更新为5
const updateDocument = {
$set: {
quantity: 5,
},
};
const result = await myColl.updateOne(filter, updateDocument);
The updated document resembles the following, with an updated value in the 更新后的文档类似于以下内容,quantity
field and all other values unchanged:quantity
字段中有一个更新的值,所有其他值不变:
{
_id: 465,
item: "Hand-thrown ceramic plate",
price: 32.50,
quantity: 5,
}
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 with the specified fields and values.更新操作可以配置为执行upsert,该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 Server手册。
Replace替换
To perform a replacement operation, create a replacement document that consists of the fields and values that you would like to use 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.替换文档是要替换与查询筛选器匹配的现有文档的文档。
Example实例
Consider a document in the 考虑myDB.items
collection with fields describing an item for sale, its price, and the quantity available:myDB.items
集合中的一个文档,该文档包含描述待售物品、价格和可用数量的字段:
{
_id: 501,
item: "3-wick beeswax candle",
price: 18.99,
quantity: 10,
}
Suppose you wanted to replace this document with one that contains a description for an entirely different item. Your replacement operation might resemble the following:假设您想用一个包含完全不同项目描述的文档来替换此文档。您的更换操作可能类似于以下内容:
const myDB = client.db("myDB");
const myColl = myDB.collection("items");
const filter = { _id: 501 };
//replace the matched document with the replacement document用替换文档替换匹配的文档
const replacementDocument = {
item: "Vintage silver flatware set",
price: 79.15,
quantity: 1,
};
const result = await myColl.replaceOne(filter, replacementDocument);
The replaced document contains the contents of the replacement document and the immutable 被替换的文档包含替换文档的内容和不可变_id
field as follows:_id
字段,如下所示:
{
_id: 501,
item: "Vintage silver flatware set",
price: 79.15,
quantity: 1,
}
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 with the specified fields and values.替换操作可以配置为执行upsert,该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 Server手册。