-
Type: New Feature
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
Fully Compatible
-
Integration 2016-08-29
-
0
As of 3.3.11, we added the $addFields aggregation stage:
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]}
Original Description
It would be great if you could use $project (or something) to decorate the input object with additional, calculated values. This way, you wouldn't need to explicitly name all the fields that you want to carry forward if you just need to add calculated values.
For example, currently, in a collection with the following document:
{_id: 0, foo: 1, bar: 1}
When you run the following pipeline:
db.coll.aggregate([{$project: {newField: {$add: [1, '$foo']}}}])
You get the following document:
{_id: 0, newField: 2}
This is requesting that there be some way to get the following document (with all original fields present), without knowing which fields are in the original document:
{_id: 0, foo: 1, bar: 1, newField: 2}
This could be accomplished by adding some option to the $project stage, e.g.
db.coll.aggregate([{$project: {newField: {$add: [1, '$foo']}, $keepExisting: true}}])
Or by adding a new stage, e.g.
db.coll.aggregate([{$set: {newField: {$add: [1, '$foo']}}])
- is depended on by
-
CSHARP-1750 Add support for $addFields aggregation stage
- Closed
-
JAVA-2283 Add builder for $addFields aggregation stage
- Closed
- is duplicated by
-
SERVER-24779 Add a $transform aggregation stage
- Closed
- is related to
-
SERVER-24743 $group aggregation command should be able to set a catch-all accumulator
- Closed
-
SERVER-18966 Allow exclusion in $project stage of aggregation pipeline
- Closed
- related to
-
SERVER-25200 $project computed fields on arrays sets every element of the array -- inconsistent with $set
- Closed
-
SERVER-25581 Leaked ParsedAddField when destructing DocumentSource for $addFields stage
- Closed
-
DRIVERS-297 Aggregation Framework Support for 3.4
- Closed
-
SERVER-8582 Extend document expression language in aggregation to support advanced document filtering.
- Closed