1

Here is the Spring Repository class

public interface UserRepository extends MongoRepository {

@Query(value = "{location :{ $nearSphere :{$geometry : {type : \"Point\", coordinates : [?1, ?0] }, $maxDistance :?2}}")
List<User> search(float latitude, float longitude, float radius, int limit);

}

I'm not able to use limit() function to limit total number of results returned by the search function. I've tried appending limit() in all places within @Query annotation.

  • Does this answer your question? [Use limit and skip in MongoRepository](https://stackoverflow.com/questions/71887036/use-limit-and-skip-in-mongorepositorycustomer-string) – Étienne Miret Jan 26 '23 at 07:57

1 Answers1

4

Using the repository abstraction allows you to add a Pageable parameter (will be automatically picked up for query execution) to the method signature.

@Query(value = "{location :{ $nearSphere :{$geometry : {type : \"Point\", coordinates : [?1, ?0] },  $maxDistance :?2}}")
List<User> search(float latitude, float longitude, float radius, Pageable page);

Once you've got that in place you can simply call it via

//get the first 10 matching results
userRepository.search(37.802066, -122.377347, 10, new PageRequest(0,10));

You can find some more samples for special parameter bindings and geo queries in the reference manual.

Christoph Strobl
  • 6,491
  • 25
  • 33