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

Equality queries on _id with projection may return no results on sharded collections

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Fixed
    • Affects Version/s: 2.6.2
    • Fix Version/s: 2.6.3, 2.7.3
    • Component/s: Querying, Sharding
    • Labels:
      None
    • Operating System:
      ALL
    • Backport Completed:
    • Steps To Reproduce:
      Hide

      sh = new ShardingTest({name:"test", shards:2})
      db = sh.getDB( "test" );
      sh.adminCommand({enableSharding:"test"})
      sh.adminCommand({shardCollection:"test.test", key: {foo:1}})
      db.test.insert({_id:1, foo:1, bar:1})
      res = db.test.find({_id:1},{bar:1}).itcount()
      assert.eq(res, 1, "Was unable to find document")

      Show
      sh = new ShardingTest({name: "test" , shards:2}) db = sh.getDB( "test" ); sh.adminCommand({enableSharding: "test" }) sh.adminCommand({shardCollection: "test.test" , key: {foo:1}}) db.test.insert({_id:1, foo:1, bar:1}) res = db.test.find({_id:1},{bar:1}).itcount() assert.eq(res, 1, "Was unable to find document" )

      Description

      Issue Status as of Jun 19, 2014

      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.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                14 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: