title: MongoDB Java client - why does `sort` seem to break my query?
body: <p>I have not been able to make a query work when using <code>sort</code>. I expect the results of a query to be exactly the same as if I were not using <code>sort</code>, except the results should come in sorted, of course, but what happens is that when using <code>sort</code> I get nothing back.</p>
  
  <p>Here's a complete example to reproduce the problem:</p>
  
  <pre><code>DB db = fongo.getDB( "something" );
  DBCollection collection = db.getCollection( "what" );
  collection.insert( new BasicDBObject( "hello", 4 ) );
  collection.insert( new BasicDBObject( "hello", 2 ) );
  collection.insert( new BasicDBObject( "hello", 1 ) );
  collection.insert( new BasicDBObject( "hello", 3 ) );
  
  final DBCursor sorted = collection
      .find( new BasicDBObject( "hello", new BasicDBObject( "$exists", true ) ) )
      .sort( new BasicDBObject( "hello", 1 ) )
      .limit( 10 );
  
  final DBCursor notSorted = collection
      .find( new BasicDBObject( "hello", new BasicDBObject( "$exists", true ) ) )
              .limit( 10 );
  
  // both asserts below work!
  assertThat( notSorted.size(), is( 4 ) );
  assertThat( sorted.size(), is( 4 ) );
  
  List&lt;DBObject&gt; notSortedAsList = notSorted.toArray();
  List&lt;DBObject&gt; sortedAsList = sorted.toArray();
  
  assertThat( notSortedAsList.size(), is( 4 ) );
  assertThat( sortedAsList.size(), is( 4 ) ); // &lt;&lt; BREAKS HERE!!!!
  assertThat( sortedAsList.stream().map( obj -&gt; obj.get( "hello" ) )
      .collect( Collectors.toList() ), is( Arrays.asList( 1, 2, 3, 4 ) ) );
  </code></pre>
  
  <p>As you can see, the <code>notSortedAsList</code> list contains 4 elements, as expected, but <code>sortedAsList</code> is empty!! The only difference being that the latter was created from a query containing <code>sort</code>.</p>
  
  <p>Unless I am doing something wrong, it seems that this could be a bug in the MongoDB Java driver, even though it could also be related to <code>Fongo</code> as I am using it to test this.</p>
  
  <p>Any ideas about what's happening??</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is what is generated by the query containing sort shown above:</p>
  
  <pre><code>find({ "query" : { "hello" : { "$exists" : true}} , "orderby" : { "hello" : 1}}, null).skip(0).limit(10)
  </code></pre>
  
  <p>Without <code>sort</code>, the query looks like this:</p>
  
  <pre><code>find({ "hello" : { "$exists" : true}}, null).skip(0).limit(10)
  </code></pre>
  
  <p>I have also tried doing the following query:</p>
  
  <pre><code>final DBCursor sorted = collection
      .find( new BasicDBObject( "hello", new BasicDBObject( "$exists", true ) ) )
      .addSpecial( "$orderby", new BasicDBObject( "hello", 1 ) )
      .limit( 10 );
  </code></pre>
  
  <p>The generated query then is:</p>
  
  <pre><code>find({ "$orderby" : { "hello" : 1} , "query" : { "hello" : { "$exists" : true}}}, null).skip(0).limit(10)
  </code></pre>
  
  <p>Both have the same result, though the first uses <code>orderby</code> and the second uses <code>$orderby</code> (as suggested here: <a href="http://docs.mongodb.org/manual/reference/operator/meta/orderby/#op._S_orderby" rel="nofollow">http://docs.mongodb.org/manual/reference/operator/meta/orderby/#op._S_orderby</a>)</p>