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

Add builder for $addFields aggregation stage

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.4.0
    • Component/s: Builders
    • Labels:
      None
    • Sprint:
      JVM Sprint 35

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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: