[SERVER-29298] certain _id subfield projections return different results depending on presence of index Created: 19/May/17  Updated: 06/Dec/22  Resolved: 02/Jun/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Charlie Swanson Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-7502 Query's projection does not obey part... Closed
Assigned Teams:
Query
Operating System: ALL
Steps To Reproduce:

> 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
}

Participants:

 Description   

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



 Comments   
Comment by David Storch [ 02/Jun/17 ]

Closing as a duplicate of SERVER-7502.

Generated at Thu Feb 08 04:20:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.