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

$project should add top-level field to dependencies for nested computed fields

    XMLWordPrintable

    Details

    • Backwards Compatibility:
      Minor Change
    • Operating System:
      ALL
    • Sprint:
      Query 2019-12-02, Query 2019-12-16, Query 2019-12-30

      Description

      The following example demonstrates an inconsistency within the $project stage:

      > db.foo.insert({a: [1, {b: 2}, 3, {}]})  // Note 4 elements in 'a'.
      WriteResult({ "nInserted" : 1 })
      > db.foo.aggregate([{$project: {"a.b": {$literal: "NEW"}}}]).pretty()
      {
      	"_id" : ObjectId("57d6d3cd150e60d4d52d9714"),
      	"a" : [  // Note there are still 4 elements in 'a'. scalar values have been replaced with documents.
      		{
      			"b" : "NEW"
      		},
      		{
      			"b" : "NEW"
      		},
      		{
      			"b" : "NEW"
      		},
      		{
      			"b" : "NEW"
      		}
      	]
      }
      > db.foo.aggregate([{$project: {"a.b.c": {$literal: "NEW"}}}]).pretty()
      {
      	"_id" : ObjectId("57d6d3cd150e60d4d52d9714"),
      	"a" : [  // Note there are only 2 values in 'a'.
      		{
      			"b" : {
      				"c" : "NEW"
      			}
      		},
      		{
      			"b" : {
      				"c" : "NEW"
      			}
      		}
      	]
      }
      

      This happens because the second projection adds 'a.b' to its dependencies instead of just 'a', which loses the 'shape' of 'a':

      > db.foo.explain().aggregate([{$project: {"a.b.c": {$literal: "NEW"}}}])
      {
      	"stages" : [
      		{
      			"$cursor" : {
      				"query" : {
      					
      				},
      				"fields" : {
      					"a.b" : 1,
      					"_id" : 1
      				},
          ...  // Other explain info.
      }
      > db.foo.find({}, {_id: 1, "a.b": 1}).pretty()
      {
      	"_id" : ObjectId("57d6d443f5f2b41fe6bb267c"),
      	"a" : [  // Note only 2 of 4 elements result from this projection.
      		{
      			"b" : 2
      		},
      		{
      			
      		}
      	]
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              ian.boros Ian Boros
              Reporter:
              charlie.swanson Charlie Swanson
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: