Saurabh Kumar
Saurabh Kumar

Reputation: 16671

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));

Upvotes: 9

Views: 19730

Answers (1)

s7vr
s7vr

Reputation: 75964

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);

Upvotes: 12

Related Questions