[JAVA-2283] Add builder for $addFields aggregation stage Created: 19/Aug/16  Updated: 27/May/22  Resolved: 13/Sep/16

Status: Closed
Project: Java Driver
Component/s: Builders
Affects Version/s: None
Fix Version/s: 3.4.0

Type: New Feature Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Justin Lee
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-5781 Implement $addFields aggregation stag... Closed
is depended on by DRIVERS-297 Aggregation Framework Support for 3.4 Closed
Epic Link: MongoDB 3.4 for Java driver
Server Compat: 3.3

 Description   

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]}


Generated at Thu Feb 08 08:56:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.