ISSUE SUMMARY
Queries on non-hashed sharded collections that include an equality predicate on the _id field and a projection to exclude the shard key erroneously return no results.
Example:
The following query includes an equality predicate on _id and contains a projection returns only the _id and bar fields. Suppose coll is a sharded collection with shard key {foo:1}. Since foo is not in the resulting document, the query is affected by this bug and won't return any results.
db.coll.find({_id:ObjectId("53a349440d3a24cdc742e1df"), baz:50}, {bar:1})
USER IMPACT
On non-hashed sharded collections, queries of this type return no results. Sharded collections on a hashed shard key are unaffected by this bug.
WORKAROUNDS
Add a hint of {_id:1} to these queries. Alternatively, it is safe to downgrade only the mongod shard servers to 2.6.1 to avoid this problem. For example:
db.coll.find({_id:ObjectId("53a349440d3a24cdc742e1df"), baz:50}, {bar:1}).hint({_id:1})
AFFECTED VERSIONS
MongoDB production release 2.6.2 is affected by this issue.
FIX VERSION
The fix is included in the 2.6.3 production release.
RESOLUTION DETAILS
On sharded connections, the IDHackRunner incorrectly applied the projection to the result document before passing it to the sharding filter (which removes orphan documents). The order of these steps has been reversed.
Original description
Sharded queries that include an equality predicate on _id and a projection that removes the shard key will return no results.
The cause of this issue is a regression introduced in 2.6.2 (SERVER-13337). On sharded connections, IDHackRunner incorrectly applies the projection to the result document before passing it to the sharding filter (which removes orphan documents); these steps need to happen in the reverse order. If the projected document being passed to the sharding filter doesn't include the shard key, then the document is concluded to be an orphan.
To work around this issue, add a hint of {_id: 1} to any affected query. An explicit hint disables use of the IDHackRunner.
- is related to
-
SERVER-14304 Equality queries on _id with projection on _id may return orphan documents on sharded collections
- Closed
-
SERVER-14320 CollectionMetadata::keyBelongsToMe() should assert that given key is non-empty in debug builds
- Closed
- related to
-
SERVER-13337 Re-enable fast path for _id equality queries with projection
- Closed