Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data MongoDB Date Between

I use spring data mongodb.

I want the records between two dates. The following MongoDB Query works:

db.posts.find({startDate: {$gte: start, $lt: end}});

My attempted Spring data query object code translation does not work:

Query query = new Query();
query.addCriteria(Criteria.where("startDate").gte(startDate)
                            .and("startDate").lt(endDate));

What is the correct order of method calls to build the Mongo query I need?

like image 559
ujava Avatar asked Apr 25 '12 07:04

ujava


People also ask

How do I extract data between two dates in spring boot?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.

What is $date in MongoDB?

Date() returns the current date as a string in mongosh. new Date() returns the current date as a Date object. mongosh wraps the Date object with the ISODate helper. The ISODate is in UTC.


4 Answers

Do not include the 'and("startDate")' part in your criteria.

Instead of :

query.addCriteria(Criteria.where("startDate").gte(startDate).and("startDate").lt(endDate));

You should use:

query.addCriteria(Criteria.where("startDate").gte(startDate).lt(endDate));

When you include the 'and("startDate")' part, Mongo see's it as two different entries on the same property.

like image 89
Yohan Liyanage Avatar answered Oct 04 '22 20:10

Yohan Liyanage


Reference here

Query query = new Query(
  Criteria.where("ip").is(ip)
  .andOperator(
    Criteria.where("createdDate").lt(endDate),
    Criteria.where("createdDate").gte(startDate)
  )
);
like image 25
DarwinFernandez Avatar answered Oct 04 '22 21:10

DarwinFernandez


You also can add query annotaion:

@Query("{'date' : { $gte: ?0, $lte: ?1 } }")                 
public List<AnyYourObj> getObjectByDate(Date from, Date to); 

Or proper spring data method signature:

public List<AnyYourObj> findByDateBetween(Date from, Date to);

Both of these approaches give the same result. You can read more here https://www.baeldung.com/queries-in-spring-data-mongodb and here https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/

like image 45
S.Dayneko Avatar answered Oct 04 '22 22:10

S.Dayneko


I had to find dates between on field publishedDate and here is how I did it:

    Criteria publishedDateCriteria = Criteria
                        .where("publishedDateObject").gte(psDate)
                        .lte(peDate);
    Query query = new Query(publishedDateCriteria);
    mongoTemplate.find(query,
                        MyDocumentObject.class));
like image 28
James Jithin Avatar answered Oct 04 '22 21:10

James Jithin