$set

On this page本页内容

Definition定义

Disambiguation消歧

The following page refers to the update operator $set. 下一页涉及更新运算符$setFor the aggregation stage $set, available starting in MongoDB 4.2, see $set.对于MongoDB 4.2中提供的聚合阶段$set,请参阅$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行为

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将更新或创建每个字段。

Examples示例

Consider a collection products with the following document:考虑一个集合products与以下文件:

{
  _id: 100,
  sku: "abc123",
  quantity: 250,
  instock: true,
  reorder: false,
  details: { model: "14Q2", make: "xyz" },
  tags: [ "apparel", "clothing" ],
  ratings: [ { by: "ijk", 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运算符更新quantity字段、details字段和tags字段的值。

db.products.update(
   { _id: 100 },
   { $set:
      {
        quantity: 500,
        details: { model: "14Q3", make: "xyz" },
        tags: [ "coats", "outerwear", "clothing" ]
      }
   }
)

The operation replaces the value of: quantity to 500; the details field to a new embedded document, and the tags field to a new array.该操作将:quantity的值替换为500;将details字段添加到新的嵌入文档,将tags字段添加到新数组。

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的文档,以下操作将更新详细信息文档中的make字段:

db.products.update(
   { _id: 100 },
   { $set: { "details.make": "zzz" } }
)

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.对于匹配条件_id等于100的文档,以下操作将更新tags字段中的值的第二个元素(数组索引为1)和ratings数组中第一个元素(数组索引为0)中的rating字段。

db.products.update(
   { _id: 100 },
   { $set:
      {
        "tags.1": "rain gear",
        "ratings.0.rating": 2
      }
   }
)

For additional update operators for arrays, see Array Update Operators.有关阵列的其他更新运算符,请参阅数组更新运算符