8

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.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
CrazyProgrammer
  • 544
  • 8
  • 29

1 Answers1

10

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");
Kees de Kooter
  • 7,078
  • 5
  • 38
  • 45
Viresh
  • 304
  • 2
  • 3