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?
This works on version 2.7.2 of the Java driver
Also, for the record you have "startDate" for both the gte and the lte parameters.
You also can add query annotaion:
Or proper spring data method signature:
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/
I did like this
https://docs.spring.io/spring-data/mongodb/docs/2.2.x-SNAPSHOT/reference/html/#mongodb.repositories.queries
Do not include the 'and("startDate")' part in your criteria.
Instead of :
You should use:
When you include the 'and("startDate")' part, Mongo see's it as two different entries on the same property.
I had to find dates between on field
publishedDate
and here is how I did it: