Definition定义
Note
Disambiguation消歧
$setThe$setoperator replaces the value of a field with the specified value.$set运算符将字段的值替换为指定值。
Compatibility兼容性
You can use 您可以将$set for deployments hosted in the following environments:$set用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The $set operator expression has the following form:$set运算符表达式具有以下形式:
{ $set: { <field1>: <value1>, ... } }
To specify a 要在嵌入式文档或数组中指定<field> in an embedded document or in an array, use dot notation.<field>,请使用点符号。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.从MongoDB 5.0开始,更新运算符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
If the field does not exist, 如果字段不存在,$set will add a new field with the specified value, provided that the new field does not violate a type constraint. $set将添加一个具有指定值的新字段,前提是新字段不违反类型约束。If you specify a dotted path for a non-existent field, 如果为不存在的字段指定虚线路径,$set will create the embedded documents as needed to fulfill the dotted path to the field.$set将根据需要创建嵌入式文档,以完成字段的虚线路径。
If you specify multiple field-value pairs, 如果指定多个字段值对,$set will update or create each field.$set将更新或创建每个字段。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当您使用带有空操作数表达式(mongod no longer raises an error when you use an update operator like $set with an empty operand expression ( { } ). { })的$set等更新运算符时,mongod不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
Examples示例
Create the 创建products collection:products集合:
db.products.insertOne(
{
_id: 100,
quantity: 250,
instock: true,
reorder: false,
details: { model: "14QQ", make: "Clothes Corp" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "Customer007", rating: 4 } ]
}
)
Set Top-Level Fields设置顶级字段
For the document matching the criteria 对于与等于100的条件_id equal to 100, the following operation uses the $set operator to update the value of the quantity field, details field, and the tags field._id匹配的文档,以下操作使用$set运算符更新quantity字段、details字段和tags字段的值。
db.products.updateOne(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: "2600", make: "Fashionaires" },
tags: [ "coats", "outerwear", "clothing" ]
}
}
)
The operation updates the:该操作将更新:
value ofquantityto500quantity值为500带有新嵌入文档的detailsfield with new embedded documentdetails字段使用新数组的tagsfield with new arraytags字段
{
_id: 100,
quantity: 500,
instock: true,
reorder: false,
details: { model: '2600', make: 'Fashionaires' },
tags: [ 'coats', 'outerwear', 'clothing' ],
ratings: [ { by: 'Customer007', rating: 4 } ]
}Set Fields in Embedded Documents在嵌入式文档中设置字段
To specify a 要在嵌入式文档或数组中指定<field> in an embedded document or in an array, use dot notation.<field>,请使用点符号。
For the document matching the criteria 对于与等于_id equal to 100, the following operation updates the make field in the details document:100的条件_id匹配的文档,以下操作将更新details文档中的make字段:
db.products.updateOne(
{ _id: 100 },
{ $set: { "details.make": "Kustom Kidz" } }
)
After updating, the document has the following values:更新后,文档具有以下值:
{
_id: 100,
quantity: 500,
instock: true,
reorder: false,
details: { model: '2600', make: 'Kustom Kidz' },
tags: [ 'coats', 'outerwear', 'clothing' ],
ratings: [ { by: 'Customer007', rating: 4 } ]
}
Important
The above code uses 上面的代码使用点符号来更新嵌入式dot notation to update the make field of the embedded details document. The code format looks similar to the following code example, which instead replaces the entire embedded document, removing all other fields in the embedded details document:details文档的make字段。代码格式类似于以下代码示例,它替换了整个嵌入式文档,删除了嵌入式详细信息文档中的所有其他字段:
db.products.updateOne(
{ _id: 100 },
{ $set: { details:
{make: "Kustom Kidz"}
}
})Set Elements in Arrays在数组中设置元素
To specify a 要在嵌入式文档或数组中指定<field> in an embedded document or in an array, use dot notation.<field>,请使用点符号。
For the document matching the criteria 对于与等于_id equal to 100, the following operation updates the value second element (array index of 1) in the tags field and the rating field in the first element (array index of 0) of the ratings array.100的criteria-_id匹配的文档,以下操作更新评级数组的标签字段中的值第二个元素(数组索引为1)和第一个元素(数组索引为0)中的评级字段。
db.products.updateOne(
{ _id: 100 },
{ $set:
{
"tags.1": "rain gear",
"ratings.0.rating": 2
}
}
)
After updating, the document has the following values:更新后,文档具有以下值:
{
_id: 100,
quantity: 500,
instock: true,
reorder: false,
details: { model: '2600', make: 'Kustom Kidz' },
tags: [ 'coats', 'rain gear', 'clothing' ],
ratings: [ { by: 'Customer007', rating: 2 } ]
}
For additional update operators for arrays, see Array Update Operators.有关数组的其他更新运算符,请参阅数组更新运算符。