On this page本页内容
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将更新或创建每个字段。
Consider a collection 考虑一个集合products with the following document:products与以下文件:
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字段的值。
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字段添加到新数组。
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字段:
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字段。
For additional update operators for arrays, see Array Update Operators.有关阵列的其他更新运算符,请参阅数组更新运算符。
See also参阅