Results 1 to 4 of 4

Thread: Quering date range from mongodb?

  1. #1
    Join Date
    Jan 2012
    Posts
    17

    Default Quering date range from mongodb?

    I need to query between 2 dates. With mongodb api, I can do:
    Code:
    query.put("plantime", new BasicDBObject("$gte",firstDate).append("$lte", secondDate));
    Now with Spring data, I've tried:
    Code:
    Criteria.where("plantime").gte(firstDate).and("plantime").lte(secondDate);
    I then received an errorr saying that "plantime" can't occur 2 times in the search. Is there any way to do things similar to "append"? or is there another way to search a range similar to "between"?

  2. #2
    Join Date
    Jan 2012
    Posts
    17

    Default

    Still can't find solution

  3. #3

    Default

    I had the same issue but this seems to works:
    Code:
    DBObject cond = QueryBuilder.start(PageViewUtil.VIEW_DATE_TIME).greaterThanEquals(start).lessThan(end).get();
    Query query = new BasicQuery(cond);
    Actually doesn't work that well - the end range is ignored
    Last edited by aislingvasey; May 29th, 2012 at 05:55 AM.

  4. #4

    Lightbulb Found a workaround for this

    This works :
    Code:
    List<Criteria> timepointCriterias = new ArrayList<>();
    timepointCriterias.add(Criteria.where("timepoint").gt(since));
    timepointCriterias.add(Criteria.where("timepoint").lte(until));
    query.addCriteria(new Criteria().andOperator(
    	timepointCriterias.toArray(new Criteria[timepointCriterias.size()]))
    );
    query.sort().on("timepoint", Order.ASCENDING);
    which actually becomes this :
    Code:
    "$and" : [ { "timepoint" : { "$gt" : "2012-07-18"}} , { "timepoint" : { "$lte" : "2012-07-20"}}]

    But i am still clueless on the spring mongo code to achieve this :
    Code:
    {"timepoint": {"$gt": "2012-07-18", "$lte": "2012-07-20"}}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •