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

Positional array projection returns wrong results for _id hack queries

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.4.12
    • Component/s: Querying
    • Labels:
      None
    • ALL
    • Hide
      [esmet@celery mongo] $ ./mongo
      MongoDB shell version: 2.4.10
      connecting to: 127.0.0.1:29001/test
      > db.test.drop()
      > db.test.insert({"_id": 0,"subDocs":[{"id":1,"foo":"bar"},{"id":2,"foo":"baz"}]})
      > db.test.find({ 'subDocs.id': 1 }, { 'subDocs.$': 1 })
      { "_id" : 0, "subDocs" : [  {  "id" : 1,  "foo" : "bar" } ] }
      > db.test.find({ _id: 0 }, { 'subDocs.$': 1 })
      { "_id" : 0, "subDocs" : [  {   },  {   } ] } // oops!
      > db.test.find({ _id: 0 })
      { "_id" : 0, "subDocs" : [  {  "id" : 1,  "foo" : "bar" },  {  "id" : 2,  "foo" : "baz" } ] }
      
      Show
      [esmet@celery mongo] $ ./mongo MongoDB shell version: 2.4.10 connecting to: 127.0.0.1:29001/test > db.test.drop() > db.test.insert({ "_id" : 0, "subDocs" :[{ "id" :1, "foo" : "bar" },{ "id" :2, "foo" : "baz" }]}) > db.test.find({ 'subDocs.id' : 1 }, { 'subDocs.$' : 1 }) { "_id" : 0, "subDocs" : [ { "id" : 1, "foo" : "bar" } ] } > db.test.find({ _id: 0 }, { 'subDocs.$' : 1 }) { "_id" : 0, "subDocs" : [ { }, { } ] } // oops! > db.test.find({ _id: 0 }) { "_id" : 0, "subDocs" : [ { "id" : 1, "foo" : "bar" }, { "id" : 2, "foo" : "baz" } ] }

      The old 2.4.X query by _id hack code path doesn't pass a MatchDetails object to fillQueryResultFromObj, so the positional array projection feature (SERVER-828) doesn't do the right thing. See:

      curop.debug().idhack = true;
      if ( found ) {
          n = 1;
          fillQueryResultFromObj( bb, pq.getFields() , resObject ); // details stays the default NULL param value
      }
      

      The easiest thing to do would be to avoid the query by _id hack altogether for queries with a positional array projection.

      Note: I do not think this bug exists in 2.6.X or 2.8.X but I haven't tried it.

            Assignee:
            Unassigned Unassigned
            Reporter:
            john.esmet@gmail.com John Esmet
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: