[SERVER-6979] hint with { $natural:-1 } returns documents in forward order Created: 09/Sep/12  Updated: 28/Jan/15  Resolved: 28/Jan/15

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Aaron Staple Assignee: David Storch
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-14459 Query with hint({$natural: -1}) retur... Closed
Related
is related to SERVER-8564 Clean up unneeded code for $natural h... Backlog
Operating System: ALL
Participants:

 Description   

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() );



 Comments   
Comment by David Storch [ 28/Jan/15 ]

jonathan.abrahams, the 2.6 behavior is correct. This issue has been fixed in the 2.6 query system. $natural with .hint() and $natural with .sort() now have the same meaning. Resolving as a duplicate of SERVER-10026.

Comment by Jonathan Abrahams [ 07/Jul/14 ]

In 2.6, hint({$natural: -1}) returns the documents in reverse order, which is different behavior than 2.4. Clarification of the behavior for $natural is required.

Generated at Thu Feb 08 03:13:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.