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

hint with { $natural:-1 } returns documents in forward order

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • ALL

      When a $natural hint is parsed, the directional argument is ignored. This can cause an unexpected result, because hinting $natural in reverse direction does a $natural query in forward direction. (Reverse natural order can be specified using sort rather than hint.) The argument passed to hint is generally used identify an index, not the direction the index should be traversed (which can be specified by sort). I think

      { $natural:-1 }

      should be treated as an invalid hint, as the schema for an unindexed scan is

      { $natural:1 }

      (different).

      c = db.c;
      c.drop();
      
      c.save( { a:0 } );
      c.save( { a:1 } );
      
      function checkResultSet( expected, actual ) {
          assert.eq( expected.length, actual.length );
          for( i = 0; i < expected.length; ++i ) {
              assert.eq( expected[ i ], actual[ i ].a );
          }
      }
      
      checkResultSet( [ 0, 1 ], c.find().sort( { $natural:1 } ).toArray() );
      checkResultSet( [ 0, 1 ], c.find().hint( { $natural:1 } ).toArray() );
      
      checkResultSet( [ 1, 0 ], c.find().sort( { $natural:-1 } ).toArray() );
      
      // This returns results as $natural:1, should uassert instead (I think).
      checkResultSet( [ 1, 0 ], c.find().hint( { $natural:-1 } ).toArray() );
      

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            aaron Aaron Staple
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: