Spring data mongodb search for ISO date

前端 未结 4 1963
余生分开走
余生分开走 2020-12-05 20:49

I am trying to search for date of birth using query

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

And spring data

相关标签:
4条回答
  • 2020-12-05 21:34
    val query = Query()
    query.addCriteria( 
    Criteria("info.dob").gte(dob.atStartOfDay())
    
    0 讨论(0)
  • 2020-12-05 21:45

    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;
            }
        }
    
    
    
    } 
    
    0 讨论(0)
  • 2020-12-05 21:48

    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

    0 讨论(0)
  • 2020-12-05 21:51

    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);
    
    0 讨论(0)
提交回复
热议问题