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

$match involving field generated from $unwind is pushed ahead of $unwind in pipeline

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.3.5
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • Fully Compatible
    • ALL
    • Query 12 (04/04/16), Query 13 (04/22/16)

      db.pos3.aggregate([{$unwind:{path:"$likes",preserveNullAndEmptyArrays:true, includeArrayIndex:"index"}},{$match:{person:"Asya",index:0}}],{explain:true})
      {
      	"waitedMS" : NumberLong(0),
      	"stages" : [
      		{
      			"$cursor" : {
      				"query" : {
      					"person" : "Asya",
      					"index" : 0
      				},
      				"queryPlanner" : {
      					"plannerVersion" : 1,
      					"namespace" : "test.pos3",
      					"indexFilterSet" : false,
      					"parsedQuery" : {
      						"$and" : [
      							{
      								"index" : {
      									"$eq" : 0
      								}
      							},
      							{
      								"person" : {
      									"$eq" : "Asya"
      								}
      							}
      						]
      					},
      					"winningPlan" : {
      						"stage" : "FETCH",
      						"filter" : {
      							"index" : {
      								"$eq" : 0
      							}
      						},
      						"inputStage" : {
      							"stage" : "IXSCAN",
      							"keyPattern" : {
      								"person" : 1,
      								"likes.cat" : 1,
      								"likes.val" : 1
      							},
      							"indexName" : "person_1_likes.cat_1_likes.val_1",
      							"isMultiKey" : true,
      							"isUnique" : false,
      							"isSparse" : false,
      							"isPartial" : false,
      							"indexVersion" : 1,
      							"direction" : "forward",
      							"indexBounds" : {
      								"person" : [
      									"[\"Asya\", \"Asya\"]"
      								],
      								"likes.cat" : [
      									"[MinKey, MaxKey]"
      								],
      								"likes.val" : [
      									"[MinKey, MaxKey]"
      								]
      							}
      						}
      					},
      					"rejectedPlans" : [ ]
      				}
      			}
      		},
      		{
      			"$unwind" : {
      				"path" : "$likes",
      				"preserveNullAndEmptyArrays" : true,
      				"includeArrayIndex" : "index"
      			}
      		}
      	],
      	"ok" : 1
      }
      

      Note that "index" field comparison was moved ahead of $unwind where it might be matching a different field than generated index from $unwind, or if no such field exists, it rejects documents which should be kept.

      Need to include match on generated index field in exceptions to switching match components around.

            Assignee:
            benjamin.murphy Benjamin Murphy
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: