MongoDB Aggregation Pipeline addFields Stage

The $addFields stage is one of the stages to be used in the MongoDB Aggregation Pipeline stages. The $addFields stage  allows to add new fields in the document. The generated output document  contains the existing fields and new fields added using $addFields stage

Point to Consider for $addFields Stage:

  • $addFields appends new fields to existing documents
  • An aggregation operation can include one or more $addFields stages
  • $addFields can be added to embedded documents having arrays using dot notation
  • $addFields can be added to an existing array field using $concatArrays

$addFields Syntax: 

{ $addFields: { <newField1>: <expression1>, <newField2>: <expression2>,... } }
Aggregation Pipeline with $addFields  example

Lets consider a collection – studentMarks with the below given documents.

studentMarks
{
_id: 1,
subject: "Computer Science",
student: "Mohit Sharma",
assignment: [ 14, 17 ],
test: [ 18, 12 ],
extraCredit: 15
}
{
_id: 2,
subject: "Computer Science",
student: "Rohan Kapoor",
assignment: [ 18,16 ],
test: [ 14,16 ],
extraCredit: 14
}

We need to add 3 new $addFields as assignmentTotal , testTotal, creditTotal to be added in the output document.

db.studentMarks.aggregate( [
{
$addFields: {
assignmentTotal: { $sum: "$assignment" } ,
testTotal: { $sum: "$test" }
}
},
{
$addFields: { totalMarks:
{ $add: [ "$assignmentTotal", "$testTotal", "$extraCredit" ] } }
}
] )

The operation returns the output documents which includes the 3 new fields

{
"_id": 1,
"subject": "Computer Science",
"student": "Mohit Sharma",
"assignment": [ 14, 17 ],
"test": [ 18, 12 ],
"extraCredit": 15
"assignmentTotal" : 31,
"testTotal" : 30,
"totalMarks" : 76
}
{
"_id": 2,
"subject": "Computer Science",
"student": "Rohan Kapoor",
"assignment": [ 18,16 ],
"test": [ 14,16 ],
"extraCredit": 14,
"assignmentTotal" : 34,
"testTotal" : 30,
"totalMarks" : 78
}
Adding Fields to an Embedded Document

Embedded documents can be added with new fields using dot notation.  Consider the below example for carModels with the provided fields in the document

{ _id: 1,
model: "Ford",
specs: { capacity: 5, wheels: 4 , doors:4}
}

{ _id: 2,
model: "Toyota",
specs: { capacity: 5, wheels: 2 , doors: 2 }
}

Add the new field gear into the embedded documents

db.carModels.aggregate( [
{
$addFields: {
"specs.gear": "automatic"
}
}
] )

The Aggregation operation includes the new field : gear in the output document

{ _id: 1,
model: "Ford",
specs: { capacity: 5, wheels: 4 , doors:4, gear: "automatic"}
}

{ _id: 2,
model: "Toyota",
specs: { capacity: 5, wheels: 2 , doors: 2 , gear: "automatic"}
}
Overwriting an existing field

If $addFields includes the existing field then the value provided in the $addField will replace the existing field value. Consider the below record for collection studentBranch

{ _id: 1, name: "Mohit Sharma", batch: "Computer Science" }

The $addField includes name as ‘John Smith”

db.studentBranch.aggregate( [
  {
    $addFields: { "branch": "Java Programming" }
  }
] )

Then the aggregation operation changes the branch value for the student

{ _id: 1, name: "Mohit Sharma", batch: "Java Programming" }
Add $addField to an Array

$addFields allow to add new element into an Array using the $concatArrays

 $concatArrays returns the concatenated array as the result

{ $concatArrays: [ <array1>, <array2>, ... ] }

consider the collection item with the below given documents

{ "_id" : 1, item: [ "icecream" ], type: [ "butterscotch", "strawberry" ] }
{ "_id" : 2, item: [ "shakes"] , type: ["apple", "banana" ] }

Add new element to type  as “chocolate”

 db.items.aggregate([
{ $match: { _id: 1 } },
{ $addFields: { type: { $concatArrays: [ "$type", [ "chocolate" ] ] } } }
])

The aggregation operation includes “chocolate” as type

{ "_id" : 1, item: [ "icecream" ], type: [ "butterscotch", "strawberry" , "chocolate"] }
{ "_id" : 2, item: [ "shakes"] , type: ["apple", "banana" ] }

$set stage:  The $set is an alias for $addFields

{ $set: { <newField1>: <expression1>, <newField2>: <expression2>,... } }

Point to Consider for $set Stage:

  • $set appends new fields to existing documents
  • An aggregation operation can include one or more $set stages
  • $set can be added to embedded documents having arrays using dot notation
  • $set can be added to an existing array field using $concatArrays
MongoDB Aggregation Pipeline addFields Stage
Scroll to top
Copy Protected by Chetan's WP-Copyprotect.