2014-10-27 27 views
0

这里的数量是在Spring库类在弹簧数据的MongoDB,我不能指定限制值来限制结果

公共接口UserRepository扩展MongoRepository {

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

}

我无法使用limit()函数来限制搜索功能返回的结果总数。我试着在@Query注释中的所有地方添加limit()。

回答

2

使用存储库抽象允许您为方法签名添加一个Pageable参数(将自动选取执行查询)。

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

一旦你在地方,你可以简单地把它通过

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

你可以找到一些更多的样本在参考手册special parameter bindingsgeo queries

+0

谢谢你的帮助。 – 2014-10-28 15:26:58