use while loop for iterating

The other method limits the number of results without taking the
constraints into account.

Fixes GH-7883