...
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
david.storch commented on Fri, 2 Jun 2017 14:57:02 +0000: Closing as a duplicate of SERVER-7502.
> 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 }