Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-5781

Implement $addFields aggregation stage for using expression language to add new fields to a document

    • 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']}}])
      

            Assignee:
            carly.robison Carly Robison
            Reporter:
            pkaeding Patrick Kaeding
            Votes:
            26 Vote for this issue
            Watchers:
            30 Start watching this issue

              Created:
              Updated:
              Resolved: