Ticket: Paging Problem

Hi

My code is below

public List<Document> getMoviesByGenre(String sortKey, int limit, int skip, String... genres) {
        // query filter
        Bson castFilter = Filters.in("genres", genres);
        // sort key
        Bson sort = Sorts.descending(sortKey);
        List<Document> movies = new ArrayList<>();
        // TODO > Ticket: Paging - implement the necessary cursor methods to support simple
        // pagination like skip and limit in the code below
        moviesCollection.find(castFilter).sort(sort).limit(limit).skip(skip).iterator().forEachRemaining(movies::add);
        return movies;
    }

And the problem is that the test testPagingByGenre() is not passing. Can someone explain me why ? The error is the following

Expected :Only the Dead
Actual :Wondrous Boccaccio

Maybe is about the dataset. Has it been changed ?Can someone send me the Validation Code?

Thanks

the order should be like this
moviesCollection.find(castFilter).skip(skip).limit(limit).sort(sort).iterator()

I know the order of skip/limit/sort is not that important for the cursor returned by find() in the shell. I am not too sure about the Java API. Could any one confirm?

Experimentation from the shell:

> db.i.find()
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : 3 }
{ "_id" : 4 }
{ "_id" : 5 }
{ "_id" : 6 }
{ "_id" : 7 }
{ "_id" : 8 }
{ "_id" : 9 }
{ "_id" : 0 }
> db.i.find().limit( 3 ).sort( { _id : -1 } ).skip( 2 )
{ "_id" : 7 }
{ "_id" : 6 }
{ "_id" : 5 }
> db.i.find().sort( { _id : -1 } ).limit(3).skip( 2 )
{ "_id" : 7 }
{ "_id" : 6 }
{ "_id" : 5 }
> db.i.find().limit(3).skip( 2 ).sort( { _id : -1 } )
{ "_id" : 7 }
{ "_id" : 6 }
{ "_id" : 5 }
> db.i.find().skip( 2 ).sort( { _id : -1 } ).limit( 3 )
{ "_id" : 7 }
{ "_id" : 6 }
{ "_id" : 5 }

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.