On this page本页内容
Disambiguation消除歧义
The following page refers to the aggregation stage 以下页面介绍了聚合阶段$set, available starting in MongoDB 4.2 For the update operator $set, see $set.$set,从MongoDB 4.2开始,更新运算符$set可以使用,请参阅$set。
$set¶New in version 4.2.版本4.2中的新功能。
Adds new fields to documents. 向文档中添加新字段$set outputs documents that contain all existing fields from the input documents and newly added fields.$set包含输入文档和新添加字段中所有现有字段的输出文档。
The $set stage is an alias for $addFields.$set阶段是$addFields的别名。
Both stages are equivalent to a 这两个阶段都相当于一个$project stage that explicitly specifies all existing fields in the input documents and adds the new fields.$project阶段,该阶段明确指定输入文档中的所有现有字段,并添加新字段。
$set has the following form:具有以下形式:
Specify the name of each field to add and set its value to an aggregation expression. 指定要添加的每个字段的名称,并将其值设置为聚合表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Important
If the name of the new field is the same as an existing field name (including 如果新字段的名称与现有字段名称(包括_id), $set overwrites the existing value of that field with the value of the specified expression._id)相同,$set将用指定表达式的值覆盖该字段的现有值。
$set appends new fields to existing documents. 将新字段附加到现有文档。You can include one or more 可以在聚合操作中包含一个或多个$set stages in an aggregation operation.$set阶段。
To add field or fields to embedded documents (including documents in arrays) use the dot notation. 要向嵌入文档(包括数组中的文档)添加一个或多个字段,请使用点表示法。See example.请参阅示例。
To add an element to an existing array field with 要使用$set, use with $concatArrays. $set将元素添加到现有数组字段中,请与$concatArrays一起使用。See example.请参阅示例。
$set Stages$set阶段¶Create a sample 使用以下内容创建一个样本scores collection with the following:scores集合:
The following operation uses two 以下操作使用两个$set stages to include three new fields in the output documents:$set阶段在输出文档中包含三个新字段:
The operation returns the following documents:该操作将返回以下文档:
Use dot notation to add new fields to embedded documents.使用点符号向嵌入的文档添加新字段。
Create a sample collection 创建具有以下内容的样本集合vehicles with the following:vehicles:
The following aggregation operation adds a new field 下面的聚合操作将一个新的字段fuel_type to the embedded document specs.fuel_type添加到嵌入的文档specs中。
The operation returns the following results:操作返回以下结果:
Specifying an existing field name in a 在$set operation causes the original field to be replaced.$set操作中指定现有字段名会导致替换原始字段。
Create a sample collection called 创建一个名为animals with the following:animals的样本集合,包括以下内容:
The following 以下$set operation overrides the cats field:$set操作将覆盖cats字段:
The operation returns the following document:该操作将返回以下文档:
It is possible to replace one field with another. 可以用另一个字段替换一个字段。In the following example the 在下面的示例中,item field substitutes for the _id field.item字段替代了_id字段。
Create a sample collection called 创建一个名为fruits contains the following documents:fruits的样本集合,其中包含以下文档:
The following aggregration operation uses 下面的聚合操作使用$set to replace the _id field of each document with the value of the item field, and replaces the item field with a string "fruit".$set将每个文档的_id字段替换为item字段的值,并将item字段替换为字符串"fruit"。
The operation returns the following:该操作返回以下内容:
Create a sample 使用以下内容创建一个样本scores collection with the following:scores集合:
You can use 可以将$set with a $concatArrays expression to add an element to an existing array field. $set与$concatArrays表达式一起使用,将元素添加到现有数组字段中。For example, the following operation uses 例如,下面的操作使用$set to replace the homework field with a new array whose elements are the current homework array concatenated with another array containing a new score [ 7 ].$set将homework字段替换为一个新数组,该数组的元素是当前homework数组与另一个包含新分数[ 7 ]的数组连接起来的数组。
The operation returns the following:该操作返回以下内容: