Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2283

Add builder for $addFields aggregation stage

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.4.0
    • 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]}
      

            Assignee:
            justin.lee Justin Lee
            Reporter:
            rathi.gnanasekaran Rathi Gnanasekaran
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: