[JAVA-1176] Calling sort in a collection query seems to make the generated-query break Created: 23/Apr/14  Updated: 11/Sep/19  Resolved: 23/Apr/14

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: 2.11.0
Fix Version/s: 2.11.1

Type: Task Priority: Major - P3
Reporter: Renato Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 64-bit, Java 8 Oracle JVM



 Description   

Copied from the StackOverflow question I created:

I have not been able to make a query work when using sort. I expect the results of a query to be exactly the same as if I were not using sort, except the results should come in sorted, of course, but what happens is that when using sort I get nothing back.

Here's a complete example to reproduce the problem:

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<DBObject> notSortedAsList = notSorted.toArray();
List<DBObject> sortedAsList = sorted.toArray();
 
assertThat( notSortedAsList.size(), is( 4 ) );
assertThat( sortedAsList.size(), is( 4 ) ); // << BREAKS HERE!!!!
assertThat( sortedAsList.stream().map( obj -> obj.get( "hello" ) )
    .collect( Collectors.toList() ), is( Arrays.asList( 1, 2, 3, 4 ) ) );

As you can see, the notSortedAsList list contains 4 elements, as expected, but sortedAsList is empty!! The only difference being that the latter was created from a query containing sort.

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 Fongo as I am using it to test this.

Any ideas about what's happening??

EDIT

This is what is generated by the query containing sort shown above:

find({ "query" : { "hello" : { "$exists" : true}} , "orderby" : { "hello" : 1}}, null).skip(0).limit(10)

Without sort, the query looks like this:

find({ "hello" : { "$exists" : true}}, null).skip(0).limit(10)

I have also tried doing the following query:

final DBCursor sorted = collection
    .find( new BasicDBObject( "hello", new BasicDBObject( "$exists", true ) ) )
    .addSpecial( "$orderby", new BasicDBObject( "hello", 1 ) )
    .limit( 10 );

The generated query then is:

find({ "$orderby" : { "hello" : 1} , "query" : { "hello" : { "$exists" : true}}}, null).skip(0).limit(10)

Both have the same result, though the first uses orderby and the second uses $orderby (as suggested here: http://docs.mongodb.org/manual/reference/operator/meta/orderby/#op._S_orderby)



 Comments   
Comment by Jeffrey Yemin [ 23/Apr/14 ]

Renato,

That AbstractMethodError will be fixed when Fongo does a release that's compatible with 2.12.

Comment by Ross Lawley [ 23/Apr/14 ]

My pleasure, follow https://github.com/fakemongo/fongo/issues/31 if you want to track when fongo supports the latest 2.12.0 driver.

Comment by Renato [ 23/Apr/14 ]

Updated my answer in StackOverflow. It works fine with Fongo 1.4.5 and Mongo driver 2.11.4. Thank you!

Comment by Renato [ 23/Apr/14 ]

Ok, I got it to work!

Using Fongo 1.4.5 (latest in Maven Central) and Mongo Java Driver 2.11.4 (which is the one used by Fongo).

As I mentioned before, it seems that the Java Driver version 2.12.0 has compatibility issues and is causing an error. But I am happy as my problem has been solved for now!

Thank you,

Renato

Comment by Ross Lawley [ 23/Apr/14 ]

Can you try 2.11.3 ? I didn't realise that Fongo has only been tested up to that version: https://github.com/fakemongo/fongo#implementation-details

Comment by Renato [ 23/Apr/14 ]

After updating to 2.12.0 I get this error:

java.lang.AbstractMethodError: com.mongodb.DBCollection.find(Lcom/mongodb/DBObject;Lcom/mongodb/DBObject;IIIILcom/mongodb/ReadPreference;Lcom/mongodb/DBDecoder;)Lcom/mongodb/QueryResultIterator;
at com.mongodb.DBCursor._check(DBCursor.java:458)
at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
at com.mongodb.DBCursor._fill(DBCursor.java:605)
at com.mongodb.DBCursor.toArray(DBCursor.java:640)
at com.mongodb.DBCursor.toArray(DBCursor.java:629)

Renato

Comment by Ross Lawley [ 23/Apr/14 ]

Hi Renato,

Thanks for the ticket I think what you are seeing is a mix of two issues. I've tested your example and it worked as expected against MongoDB 2.4.10 and the latest MongoDB 2.6.0

So I think it might be that fongo has already been updated to work to spec and is causing the assertion error. Happily the java driver was also updated in 2.11.1 (See: JAVA-802).

Can you upgrade your java driver version to confirm? The latest version is 2.12.0 which fully supports the new features available in MongoDB 2.6.0 as well as older versions of MongoDB.

Ross

Generated at Thu Feb 08 08:53:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.