2014-10-27 81 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