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

certain _id subfield projections return different results depending on presence of index

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Query
    • ALL
    • Hide
      > db.foo.find()
      { "_id" : { "x" : 1, "y" : 1 } }
      > db.foo.find({}, {"_id.x": 1})
      { "_id" : { "x" : 1, "y" : 1 } }
      > db.foo.find({"_id.x": 1}, {_id: 0, "_id.x": 1})  // This projection doesn't really make sense, but is accepted by the server.
      {  }  // Entire _id excluded.
      > db.foo.ensureIndex({"_id.x": 1})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > db.foo.find({"_id.x": 1}, {_id: 0, "_id.x": 1})
      { "_id" : { "x" : 1 } }  // A covered query allows only the 'x' subfield to be included.
      > db.foo.explain().find({"_id.x": 1}, {_id: 0, "_id.x": 1})
      {
      	"queryPlanner" : {
      		"plannerVersion" : 1,
      		"namespace" : "test.foo",
      		"indexFilterSet" : false,
      		"parsedQuery" : {
      			"_id.x" : {
      				"$eq" : 1
      			}
      		},
      		"winningPlan" : {
      			"stage" : "PROJECTION",
      			"transformBy" : {
      				"_id" : 0,
      				"_id.x" : 1
      			},
      			"inputStage" : {
      				"stage" : "IXSCAN",
      				"keyPattern" : {
      					"_id.x" : 1
      				},
      				"indexName" : "_id.x_1",
      				"isMultiKey" : false,
      				"multiKeyPaths" : {
      					"_id.x" : [ ]
      				},
      				"isUnique" : false,
      				"isSparse" : false,
      				"isPartial" : false,
      				"indexVersion" : 2,
      				"direction" : "forward",
      				"indexBounds" : {
      					"_id.x" : [
      						"[1.0, 1.0]"
      					]
      				}
      			}
      		},
      		"rejectedPlans" : [ ]
      	},
      	"serverInfo" : {
      		"host" : "franklinia",
      		"port" : 27017,
      		"version" : "0.0.0",
      		"gitVersion" : "unknown"
      	},
      	"ok" : 1
      }
      
      Show
      > db.foo.find() { "_id" : { "x" : 1, "y" : 1 } } > db.foo.find({}, { "_id.x" : 1}) { "_id" : { "x" : 1, "y" : 1 } } > db.foo.find({ "_id.x" : 1}, {_id: 0, "_id.x" : 1}) // This projection doesn't really make sense, but is accepted by the server. { } // Entire _id excluded. > db.foo.ensureIndex({ "_id.x" : 1}) { "createdCollectionAutomatically" : false , "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.foo.find({ "_id.x" : 1}, {_id: 0, "_id.x" : 1}) { "_id" : { "x" : 1 } } // A covered query allows only the 'x' subfield to be included. > db.foo.explain().find({ "_id.x" : 1}, {_id: 0, "_id.x" : 1}) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.foo" , "indexFilterSet" : false , "parsedQuery" : { "_id.x" : { "$eq" : 1 } }, "winningPlan" : { "stage" : "PROJECTION" , "transformBy" : { "_id" : 0, "_id.x" : 1 }, "inputStage" : { "stage" : "IXSCAN" , "keyPattern" : { "_id.x" : 1 }, "indexName" : "_id.x_1" , "isMultiKey" : false , "multiKeyPaths" : { "_id.x" : [ ] }, "isUnique" : false , "isSparse" : false , "isPartial" : false , "indexVersion" : 2, "direction" : "forward" , "indexBounds" : { "_id.x" : [ "[1.0, 1.0]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "franklinia" , "port" : 27017, "version" : "0.0.0" , "gitVersion" : "unknown" }, "ok" : 1 }

      The server accepts a query with a projection that specifies both _id and a subfield of _id, such as {_id: 0, "_id.x": 1}. The results of such a query will change depending on whether there is an index available for the subfield of _id.

      For context, the aggregation system disallows such a projection:

      > db.foo.aggregate([{$project: {_id: 0, "_id.x": 1}}])
      assert: command failed: {
      	"ok" : 0,
      	"errmsg" : "Invalid $project specification: specification contains two conflicting paths. Cannot specify both '_id.x' and '_id': { _id: 0.0, _id.x: 1.0 }",
      	"code" : 40176,
      	"codeName" : "Location40176"
      } : aggregate failed
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:16:14
      assert.commandWorked@src/mongo/shell/assert.js:403:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1332:5
      @(shell):1:1
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: