gary gary - 3 months ago 32
Java Question

Spring data mongo @Query bind date

I have a spring mongo repository with the following method

@Query(value = "{'topicId':{$in: ?0},'ts':{$gt:{$date:?2}, $lte:{$date:?3}}, 'status': {$ne:?4}, 'seen':?1 }")
public Page<NotificationReference> findByTopicIdInAndSeenAndStatusNot(List<String> topicIds, Boolean seen,
Date from, Date to, String status, Pageable pageable);


But the ?2 and ?3 date variables are not resolving to their values. I'm passing a
java.util.Date
to the method, how do I get spring to resolve date variables for mongo queries?

Answer

Not using $date should be enough.

 @Query(value = "{'topicId':{$in: ?0},'ts':{$gt:{$date:?2}, $lte:{$date:?3}}, 'status': {$ne:?4}, 'seen':?1 }")