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

Permit empty objects as expressions in $set and $addFields aggregation stages

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • None
    • 6.1.0-rc0
    • Querying
    • None
    • Fully Compatible
    • QE 2022-06-27, QE 2022-07-11

    Description

      Permit empty objects as expressions in $set  and $addFields aggregation stages. For example, an aggregation stage definition

       {$set: {a: {}}} 

      should be accepted and should add field "a: {}" to the resulting object (this looks like an intuitively expected system behavior). Nesting of objects should be supported, like in a following example: 

      {$set: {a: {b: {}}}}
      

       Currently, to achieve this behavior a wrapper expression $literal has to be used like this:

      {$set: {a: {$literal: {}}}}

      But an aggregation stage definition 

      {$set: {a: []}} 

      is accepted and results in field "a: []", which is somewhat inconsistent with handling of empty objects.

      $set update operator expression with a field with an empty object  

      db.foo.updateOne({}, {$set: { a: {} }})

      is accepted.

      Furthermore, we have recently relaxed restrictions for update modifiers (SERVER-38909) and $addFields (SERVER-48890)(permits {$set: {}}) to permit empty documents, thus this improvement would increase consistency.

      Currently, execution of a $set aggregation stage with an empty object as an expression, like 

      db.foo.updateOne({}, [{$set: { a: {} }}])
      

      fails with the following error message: "Invalid $set :: caused by :: an empty object is not a valid value. Found empty object at path a".

      Attachments

        Issue Links

          Activity

            People

              naomie.gao@mongodb.com Naomie Gao (Inactive)
              mindaugas.malinauskas@mongodb.com Mindaugas Malinauskas
              Votes:
              4 Vote for this issue
              Watchers:
              15 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: