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

order of find projection with field and dotted field changes results

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.6, 4.1.8
    • Component/s: Querying
    • None
    • ALL
    • Hide

      Run this code using mongo shell on an empty server.

      This has the same results on server versions 4.0.6 and 4.1.8.

      db.test.insert({
          'foo': {
              'bar': 0,
              'baz': 1
          }
      });
      for (projection of [
          {_id: 0, foo: 1},
          {_id: 0, foo: 1, 'foo.bar': 1},
          {_id: 0, 'foo.bar': 1, foo: 1},
      ]) {
          printjson(db.test.findOne({}, projection));
      }

      Output:

      MongoDB shell version v4.0.0
      connecting to: mongodb://127.0.0.1:27017
      MongoDB server version: 4.0.6
      { "foo" : { "bar" : 0, "baz" : 1 } }
      { "foo" : { "bar" : 0 } }
      { "foo" : { "bar" : 0, "baz" : 1 } }
      

       

      Show
      Run this code using mongo shell on an empty server. This has the same results on server versions 4.0.6 and 4.1.8. db.test.insert({ 'foo' : { 'bar' : 0, 'baz' : 1 } }); for (projection of [ {_id: 0, foo: 1}, {_id: 0, foo: 1, 'foo.bar' : 1}, {_id: 0, 'foo.bar' : 1, foo: 1}, ]) { printjson(db.test.findOne({}, projection)); } Output: MongoDB shell version v4.0.0 connecting to: mongodb: //127.0.0.1:27017 MongoDB server version: 4.0.6 { "foo" : { "bar" : 0, "baz" : 1 } } { "foo" : { "bar" : 0 } } { "foo" : { "bar" : 0, "baz" : 1 } }  

      When querying documents, the order of the keys in the projection object change the results when it contains both a key and its sub-key.

      The reporter could not find documentation for this behavior, so it seems unintended.

      The reporter would expect all these projections to be equal:

      {foo: 1}
      {foo: 1, 'foo.bar': 1}
      {'foo.bar': 1, foo: 1}
      

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            allegroai Roee Nizan
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: