How to add orderby using @query in mongodb repository

左心房为你撑大大i 提交于 2020-01-02 03:23:05

问题


I want to add orderby to the following repository method in mongodb with spring. I tried in various methods, but didnt work

public interface StageRepository extends MongoRepository<Stage, String> {

     @Query("{$and: [ { 'categoryId': { $eq: ?0 } }, { 'isDeleted': { $eq: ?1 } } ]}")
     public List<Stage> findByCategoryIdAndIsNotDeleted(String categoryId, Boolean deleted);

}

I want to add orderby 'order' in the query.

Not sure how to do it.


回答1:


You can do like :

@Query("{$and: [ { 'categoryId': { $eq: ?0 } }, { 'isDeleted': { $eq: ?1 } } ]}")
public List<Stage> findByCategoryIdAndIsNotDeleted(String categoryId, Boolean deleted,org.springframework.data.domain.Sort sort);

And When you call this method ,create Sort object like below:

Sort sort = new Sort(Direction.ASC/DESC,"order");


来源:https://stackoverflow.com/questions/33016151/how-to-add-orderby-using-query-in-mongodb-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!