Spring data mongodb search for ISO date

22,185

Solution 1

This code should work fine for what you need:

criteria = Criteria.where("dob").lte(new java.util.Date());

My test is with following code, which does work fine:

Lis<User> users = mongoOps.find(query(where("isActive").is(true).and("CreatedDate").lte(new java.util.Date())), User.class);

Solution 2

Query would execute perfect from Spring data mongodb0

{ "dob" : { "$lte" : { "$date" : "2015-05-16T07:55:23.257Z"}}}.

But It will not execute from mongo CLI.

Query to execute on cli.

{dob:ISODate("2015-05-15T07:55:23.257Z")}

Thanks

Solution 3

For IDE use ISODate() for SPRING DAO convert string to Date object

query.addCriteria(Criteria.where("created").ne(null).andOperator(
                Criteria.where("created").gte(DateUtils.getDate("2016-04-14 00:00:00", DateUtils.DB_FORMAT_DATETIME)),
                Criteria.where("created").lte(DateUtils.getDate("2016-04-14 23:59:59", DateUtils.DB_FORMAT_DATETIME))
            ));


public final class DateUtils {    

    public static final String DB_FORMAT_DATETIME = "yyyy-M-d HH:mm:ss";        

    private DateUtils() {
        // not publicly instantiable
    }       

    public static Date getDate(String dateStr, String format) {
        final DateFormat formatter = new SimpleDateFormat(format);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {                
            return null;
        }
    }



} 
Share:
22,185
Deepak Agrawal
Author by

Deepak Agrawal

Assistant Consultant-Developer at Oodles Technologies Pvt Ltd. I am a creative programmer. I started programming because I love logic, and it allows me to exercise my brain by wrapping it around new challenges all the time. I am a master-level graduate in computer science, I have one bachelor's degrees. I am an enthusiast of the Bitcoin project, as well as a gamer at my leisure time. I love learning new things, I love being challenged - both at work and at my free time. I strive for perfection.

Updated on September 08, 2020

Comments

  • Deepak Agrawal
    Deepak Agrawal over 3 years

    I am trying to search for date of birth using query

    criteria = Criteria.where("dob").lte(new DateTime().toDate());
    

    And spring data mongodb generate below query:

    MongoTemplate: find using query:

    { "dob" : { "$lte" : { "$date" : "2015-05-16T07:55:23.257Z"}}}
    

    fields: null for class: class com.temp.model.User in collection: user

    But I did not get any result.

    My dob field in mongodb:

    {"dob" : ISODate("1991-01-23T00:00:00Z")}
    

    How I can search for dob in ISODate format ?

  • Rémy J
    Rémy J about 8 years
    Work perfectly from Spring mongo Query annotation.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 7 years
    This doesn't deal with T and Z in the date format :(
  • user666
    user666 over 5 years
    What if we are using JPA MongoRepository? how can we call the $date or isodate without getting a parse exception in @Query annotation?
  • mrinal
    mrinal about 5 years
    If there is an index on dob will that be picked since it is translated to $date?