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

Excluding non-existent field in $project can adversely impact downstream pipeline stages

    • Minor Change
    • ALL
    • Query 2019-12-30, Query 2020-01-13, Query 2020-01-27
    • 0

      A $project stage which excludes a field that does not exist in the input document naively "removes" that field from the output document. In practice, this has the effect of adding a new field to the output with value "missing". While this is usually invisible to the client, since the "missing" field will be discarded when the document is serialized to BSON, the unexpected addition of this new field can negatively impact the behaviour of downstream pipeline stages.

      In the example below, for instance, we exclude a non-existent field b before attempting to insert new fields a,b,c into the document via the $addFields stage. Normally the new fields are added in the order they are specified, but because the exclusion has already spuriously added an empty field b, the ordering of the fields in the output document changes to b,a,c.

      Inclusions of non-existent fields do not produce these "missing" entries, and therefore do not manifest this bug.

      > db.testing.insert({_id: 1})
      WriteResult({ "nInserted" : 1 })
      
      > db.testing.aggregate([{$addFields: {a: "1", b: "2", c: "3"}}])
      { "_id" : 1, "a" : "1", "b" : "2", "c" : "3" }
      
      > db.testing.aggregate([{$project: {b: 1}}, {$addFields: {a: "1", b: "2", c: "3"}}])
      { "_id" : 1, "a" : "1", "b" : "2", "c" : "3" }
      
      > db.testing.aggregate([{$project: {b: 0}}, {$addFields: {a: "1", b: "2", c: "3"}}])
      { "_id" : 1, "b" : "2", "a" : "1", "c" : "3" }
      

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            bernard.gorman@mongodb.com Bernard Gorman
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: