Docs HomeMongoDB Manual

$set

Definition定义

Note

Disambiguation消除歧义

The following page refers to the update operator $set. For the aggregation stage $set, available starting in MongoDB 4.2, see $set.以下页面涉及更新运算符$set。有关聚合阶段$set(从MongoDB 4.2开始提供),请参阅$set

$set

The $set operator replaces the value of a field with the specified value.$set运算符将字段的值替换为指定的值。

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. 从MongoDB 5.0开始,更新运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为

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, mongod no longer raises an error when you use an update operator like $set with an empty operand expression ( { } ). 从MongoDB 5.0开始,当您使用带有空操作数表达式({ })的更新运算符(如$set)时,mongod不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建操作日志条目(这意味着该操作是无操作)。

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 _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等于100的文档,以下操作使用$set运算符更新数量字段、详细信息字段和标签字段的值。

db.products.updateOne(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: "2600", make: "Fashionaires" },
tags: [ "coats", "outerwear", "clothing" ]
}
}
)

The operation updates the:该操作将更新:

  • value of quantity to 500quantity值为500
  • details field with new embedded document具有新嵌入文档的details字段
  • tags field with new array带有新数组的tags字段
{
_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:对于匹配条件_id等于100的文档,以下操作更新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 } ]
}

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的标准_id的文档,以下操作更新ratings数组的标签字段中的值第二个元素(数组索引为1)和第一个元素(数组索引为0)中的ratings字段。

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.有关数组的其他更新运算符,请参阅数组更新运算符