0

Working with a Spring Boot and Spring data, I have this Mongo Doc:

@Document(collection = "dogs")
public class Dog{

  @Id
  private long dogId;
  private LocalDateTime creationDate;
  ...
}

I added this method to the Repository:

@Repository
public interface DogRepository extends CrudRepository<Dog, Long>, PagingAndSortingRepository<Dog, Long> {


    Page<Dog> findAllByCreationDateAfterAndCreationDateBefore(LocalDateTime createdAfter, LocalDateTime createdBefore, Pageable pageable);

When trying to access the method in run time, I get the following error:

"Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'creationDate' expression specified as 'creationDate : Document{{$lt=2021-05-12T09:42:08.549}}'. Criteria already contains 'creationDate : Document{{$gt=2021-05-12T09:42:07.486}}'."

riorio
  • 6,500
  • 7
  • 47
  • 100
  • You can use BETWEEN as shown in this post: https://stackoverflow.com/questions/42189770/mongorepository-query-for-between-dates – prasad_ May 12 '21 at 08:13

2 Answers2

0

To solve this there was a need to change the Spring-Data method, and to use Between:

Page<Dog> findAllByCreationDateBetween(LocalDateTime createdAfter, LocalDateTime createdBefore, Pageable pageable);
riorio
  • 6,500
  • 7
  • 47
  • 100
0

**FOR MULTIPLE CRITERIA YOU CAN USE COMMA...

Query query = new Query( Criteria.where("ip").is(ip) .andOperator( Criteria.where("createdDate").lt(endDate), Criteria.where("createdDate").gte(startDate) ) );**

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '23 at 09:48