On this page本页内容
$addFields
Adds new fields to documents. 向文档中添加新字段。$addFields outputs documents that contain all existing fields from the input documents and newly added fields.输出包含输入文档中所有现有字段和新添加字段的文档。
The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.$addFields阶段相当于$project阶段,它显式指定输入文档中的所有现有字段并添加新字段。
Starting in version 4.2, MongoDB adds a new aggregation pipeline stage 从版本4.2开始,MongoDB添加了一个新的聚合管道阶段$set that is an alias for $addFields.$set,它是$addFields的别名。
$addFields has the following form:具有以下形式:
{ $addFields: { <newField>: <expression>, ... } }
Specify the name of each field to add and set its value to an aggregation expression. 指定要添加的每个字段的名称,并将其值设置为聚合表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If the name of the new field is the same as an existing field name (including 如果新字段的名称与现有字段名称(包括_id), $addFields overwrites the existing value of that field with the value of the specified expression._id)相同,$addFields将用指定表达式的值覆盖该字段的现有值。
$addFields appends new fields to existing documents. 将新字段追加到现有文档。You can include one or more 可以在聚合操作中包含一个或多个$addFields stages in an aggregation operation.$addFields阶段。
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 要使用$addFields, use with $concatArrays. $addFields将元素添加到现有数组字段,请使用$concatArrays。See example.参见示例。
$addFields Stages$addFields阶段A collection called 名为scores contains the following documents:scores的集合包含以下文档:
{
_id: 1,
student: "Maya",
homework: [ 10, 5, 10 ],
quiz: [ 10, 8 ],
extraCredit: 0
}
{
_id: 2,
student: "Ryan",
homework: [ 5, 6, 5 ],
quiz: [ 8, 8 ],
extraCredit: 8
}
The following operation uses two 以下操作使用两个$addFields stages to include three new fields in the output documents:$addFields阶段在输出文档中包括三个新字段:
db.scores.aggregate( [
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: { totalScore:
{ $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
] )
The operation returns the following documents:操作将返回以下文档:
{
"_id" : 1,
"student" : "Maya",
"homework" : [ 10, 5, 10 ],
"quiz" : [ 10, 8 ],
"extraCredit" : 0,
"totalHomework" : 25,
"totalQuiz" : 18,
"totalScore" : 43
}
{
"_id" : 2,
"student" : "Ryan",
"homework" : [ 5, 6, 5 ],
"quiz" : [ 8, 8 ],
"extraCredit" : 8,
"totalHomework" : 16,
"totalQuiz" : 16,
"totalScore" : 40
}
Use dot notation to add new fields to embedded documents. 使用点符号向嵌入文档添加新字段。A collection called 一个名为vehicles contains the following documents:vehicles的集合包含以下文档:
{ _id: 1, type: "car", specs: { doors: 4, wheels: 4 } }
{ _id: 2, type: "motorcycle", specs: { doors: 0, wheels: 2 } }
{ _id: 3, type: "jet ski" }
The following aggregation operation adds a new field 以下聚合操作将新字段fuel_type to the embedded document specs.fuel_type添加到嵌入文档规范中。
db.vehicles.aggregate( [
{
$addFields: {
"specs.fuel_type": "unleaded"
}
}
] )
The operation returns the following results:该操作返回以下结果:
{ _id: 1, type: "car",
specs: { doors: 4, wheels: 4, fuel_type: "unleaded" } }
{ _id: 2, type: "motorcycle",
specs: { doors: 0, wheels: 2, fuel_type: "unleaded" } }
{ _id: 3, type: "jet ski",
specs: { fuel_type: "unleaded" } }
Specifying an existing field name in an 在$addFields operation causes the original field to be replaced.$addFields操作中指定现有字段名会导致替换原始字段。
A collection called 一个名为animals contains the following document:animals的集合包含以下文档:
{ _id: 1, dogs: 10, cats: 15 }
The following 以下$addFields operation specifies the cats field.$addFields操作指定了cats字段。
db.animals.aggregate( [
{
$addFields: { "cats": 20 }
}
] )
The operation returns the following document:运算返回以下文档:
{ _id: 1, dogs: 10, cats: 20 }
It is possible to replace one field with another. 可以用另一个字段替换一个字段。In the following example the 在以下示例中,item field substitutes for the _id field.item字段替换_id字段。
A collection called 一个名为fruit contains the following documents:fruit的集合包含以下文档:
{ "_id" : 1, "item" : "tangerine", "type" : "citrus" }
{ "_id" : 2, "item" : "lemon", "type" : "citrus" }
{ "_id" : 3, "item" : "grapefruit", "type" : "citrus" }
The following aggregration operation uses 以下聚合操作使用$addFields to replace the _id field of each document with the value of the item field, and replaces the item field with a static value.$addFields将每个文档的_id字段替换为item字段的值,并将item字段替换为静态值。
db.fruit.aggregate( [
{
$addFields: {
_id : "$item",
item: "fruit"
}
}
] )
The operation returns the following:运算结果如下:
{ "_id" : "tangerine", "item" : "fruit", "type" : "citrus" }
{ "_id" : "lemon", "item" : "fruit", "type" : "citrus" }
{ "_id" : "grapefruit", "item" : "fruit", "type" : "citrus" }
Create a sample 创建具有以下内容的示例scores collection with the following:scores集合:
db.scores.insertMany([
{ _id: 1, student: "Maya", homework: [ 10, 5, 10 ], quiz: [ 10, 8 ], extraCredit: 0 },
{ _id: 2, student: "Ryan", homework: [ 5, 6, 5 ], quiz: [ 8, 8 ], extraCredit: 8 }
])
You can use 可以使用$addFields with a $concatArrays expression to add an element to an existing array field. $addFields和$concatArrays表达式将元素添加到现有数组字段。For example, the following operation uses 例如,以下操作使用$addFields 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 ].$addFields将homework字段替换为一个新数组,该数组的元素是当前homework数组与包含新分数[7]的另一个数组串联。
db.scores.aggregate([
{ $match: { _id: 1 } },
{ $addFields: { homework: { $concatArrays: [ "$homework", [ 7 ] ] } } }
])
The operation returns the following:运算结果如下:
{ "_id" : 1, "student" : "Maya", "homework" : [ 10, 5, 10, 7 ], "quiz" : [ 10, 8 ], "extraCredit" : 0 }