-
Type: New Feature
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Builders
-
None
Syntax
{$addFields: {newFieldName1: <Expression1>, ...}}
Examples
// ===== Example #1 - Adding a literal field value ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {newField: “hello”}}]); {_id: 0, a: 1, newField: “hello”} // ===== Example #2 - Adding a value from a field path ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {b: “$a”}}]) {_id: 0, a: 1, b: 1} // ===== Example #3 - Adding a value from a system variable ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {this: “$$CURRENT”}}]) {_id: 0, a: 1, this: {_id: 0, a: 1}} // ===== Example #4 - Adding a value from an expression object ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {myNewField: {c: 3, d: 4}}}]) {_id: 0, a: 1, myNewField: {c: 3, d: 4}} // ===== Example #5 - Adding a value from an operator expression ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {alt3: {$lt: [“$a”, 3]}}}]) {_id: 0, a: 1, alt3: true} // ===== Example #6 Adding multiple new fields at once ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {b: 3, c: 5}}]) {_id: 0, a: 1, b: 3, c: 5} // ===== Example #6 - Setting a field that already exists ===== >db.example.insert({_id: 0, a: 1}) >db.example.aggregate([{$addFields: {a: [1, 2, 3]}}]) {_id: 0, a: [1, 2, 3]}
- depends on
-
SERVER-5781 Implement $addFields aggregation stage for using expression language to add new fields to a document
- Closed
- is depended on by
-
DRIVERS-297 Aggregation Framework Support for 3.4
- Closed