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

Add a $transform aggregation stage

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Sprint:
      Integration 17 (07/15/16)

      Description

      Syntax:

      $transform: {
      	root: <expression evaluates to object>,
      	set: {<fieldname>: <arbitrary expression>}
      }
      

      Examples

      // =====  Example #1 - Promoting a subfield to the top level =====
      >db.example.insert({_id: 0, a1: {b: 1}, a2: 2})
      >db.example.aggregate([{$transform: {root: “$a1”}]);
      {b: 1}
       
      // =====  Example #2 - Creating a new field =====
      >db.example.insert({_id: 0, a1: {b: 1}, a2: 2})
      >db.example.aggregate([{$transform: {set: {a3: 3}}}])
      {_id: 0, a1: {b: 1}, a2: 2, a3: 3}
       
      // =====  Example #3 - Creating new calculated fields =====
      >db.example.insert({_id: 0, a1: {b: 1}, a2: 2})
      >db.example.aggregate([{$transform: {set: {a3: {$gt: ["$a2", 3]}, a4: 4 }}}])
      {_id: 0, a1: {b: 1}, a2: 2, a3: false, a4: 4}
       
      // =====  Example #4 - Promoting a subfield to the top level and then creating new fields =====
      >db.example.insert({_id: 0, a1: {b: 1}, a2: 2 })
      >db.example.aggregate([{$transform: {root: "$a1", set: {c: 3}}}])
      {b: 1, c: 3}
       
      // =====  Example #5 - Error: using value of a field that doesn't exist anymore =====
      >db.example.insert({_id: 0, a1: {b: 1}, a2: 2 })
      >db.example.aggregate([{$transform: {root: "$a1", set: {c: "$a2"}}}])
      Error
      // Allowing this is a non-goal of this ticket.
      

      This command will be another stage in the aggregation pipeline. Either the root or set fields (or both) must be present in the $transform. The transformation replaces the document with the root field first, and then sets the fields specified in set on the contents of the replaced document.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              carly.robison Carly Robison
              Reporter:
              carly.robison Carly Robison
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: