Java/MongoDB query by date

后端 未结 6 1393
南旧
南旧 2020-12-01 10:04

I stored a value as a java.util.Date() in my collection, but when I query to get values between two specific dates, I end up getting values outside of the range. Here\'s my

6条回答
  •  有刺的猬
    2020-12-01 10:44

    Search Date with specific format following steps are that this also work in mongo 3.0

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy.MM.dd");
    

    First convert your date in your specific format, you can use any format and parse your date with above format and then pass in query.

    String date ="2014.01.02"; 
    String data1 ="2014.01.10";
    Date startDate = simpleDateFormat.parse(date);  
    Date endDate = simpleDateFormat.parse(date1);
    
    BasicDBObject query = new BasicDBObject("fieldName",
       new BasicDBObject("$gte",startDate).append("$lt",endDate ));
    

    So base on your requirement you can search date by giving argument in BasicDBobject.

提交回复
热议问题