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

aggregation requests generated field name from query

    • Query Optimization
    • Fully Compatible
    • Query 2017-03-27, Query 2017-04-17, QO 2023-03-20
    • 151

      Aggregation pipeline sends down to query fields to return which are generated later in the pipeline (as result of "as" in $lookup).

      Reproduce:

      db.companies.explain().aggregate({$match:{_id:"14"}},{$graphLookup:{from:"companies",startWith:"$_id",connectFromField:"_id",connectToField:"parent",as:"tree",maxDepth:5, depthField:"order"}},{$group:{"_id":"$tree.country"}})
      {
      	"waitedMS" : NumberLong(0),
      	"stages" : [
      		{
      			"$cursor" : {
      				"query" : {
      					"_id" : "14"
      				},
      				"fields" : {
      					"tree.country" : 1,
      					"_id" : 1
      				},
      				"queryPlanner" : {
      					"plannerVersion" : 1,
      					"namespace" : "d.companies",
      					"indexFilterSet" : false,
      					"parsedQuery" : {
      						"_id" : {
      							"$eq" : "14"
      						}
      					},
      					"winningPlan" : {
      						"stage" : "PROJECTION",
      						"transformBy" : {
      							"tree.country" : 1,
      							"_id" : 1
      						},
      						"inputStage" : {
      							"stage" : "IDHACK"
      						}
      					},
      					"rejectedPlans" : [ ]
      				}
      			}
      		},
      		{
      			"$graphLookup" : {
      				"from" : "companies",
      				"as" : "tree",
      				"connectToField" : "parent",
      				"connectFromField" : "_id",
      				"startWith" : "$_id",
      				"depthField" : "order",
      				"maxDepth" : NumberLong(5)
      			}
      		},
      		{
      			"$group" : {
      				"_id" : "$tree.country"
      			}
      		}
      	],
      	"ok" : 1
      }
      

      Note "tree.country" which is coming from the lookup and not original document.

      It appears that if project or group needs a field after lookup, there is no filtering out of fields that start with string passed as "as:" to $lookup and it ends up being pushed down into the query.

            Assignee:
            alyssa.clark@mongodb.com Alyssa Clark
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            20 Start watching this issue

              Created:
              Updated:
              Resolved: