Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoRepository query for between dates

My pojo

public class PacketData implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private final String token = UUID.randomUUID().toString();

    private final ZonedDateTime arrived = ZonedDateTime.now();
}

I plan to use like following.

@Query("?")
List<PacketData> findPacketArrivedBetween(ZonedDateTime startDate, ZonedDateTime endDate);

Is there a way i can put the following query to the above query annotation and how i can do greater than and less than logic

Query query = new Query().addCriteria(Criteria.where("arrived").gte(startDate).lte(endDate));
like image 912
Saurabh Kumar Avatar asked Feb 12 '17 15:02

Saurabh Kumar


1 Answers

You can try following couple of ways.

Without Query Annotation.

List<PacketData> findByArrivedBetween(ZonedDateTime startDate, ZonedDateTime endDate);

With Query Annotation.

@Query("{'arrived': {$gte: ?0, $lte:?1 }}")
List<PacketData> findPacketArrivedBetween(ZonedDateTime startDate, ZonedDateTime endDate);
like image 70
s7vr Avatar answered Oct 03 '22 23:10

s7vr