Skip to content

Scroll API - Example Code Problem and Question #3011

@hayscoder

Description

@hayscoder

The example code can be found here for the new Scroll API:
https://github.com/spring-projects/spring-data-commons/wiki/Spring-Data-2023.0-%28Ullman%29-Release-Notes#scroll-api

However, the way that I am reading the code example, it seems to me that it would have an endless loop. If there are more than 20 records, then won't the second instantiation of "users" always result in the same item? It is going to the offset of the SIZE (in bold below) of the users object (should always result in 10 unless there is less than 20 items total) and then minus 1. So, won't it just keep getting the second result set over and over (objects 9-19 in an array)? Shouldn't it instead have a counter that is incremented by 10 each time it goes through the for loop?

Also, can anyone give an example where you use a MongoTemplate and a query object? We don't use the direct queries as the current example has and so I am wondering how you might use the feature with a MongoTemplate and a query object.

Thank you for the help!

Window<User> users = repository.findFirst10ByLastnameOrderByFirstname("Doe", ScrollPosition.offset());
do {

  for (User u : users) {
    // consume the user
  }

  // obtain the next Scroll
  users = repository.findFirst10ByLastnameOrderByFirstname("Doe", users.positionAt(**users.size()** - 1));
} while (!users.isEmpty() && users.hasNext());

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions