javaspringmongodbmongotemplate

MongoDb Date error Java


i'm experiencing problem on a BasicQuery with Sping

Here is my query :

 public List<Voyage> getVoyages(String destination, Date datedebut, int duree)
            throws MongoException {
        Query query = new Query();
        Calendar c = Calendar.getInstance();
        c.setTime(datedebut);
        c.add(Calendar.DATE,duree);

        BasicQuery q= new BasicQuery("{$or : [{paysDestination : {$elemMatch : {nom : '"+destination+"'}}},{paysPrincipal : {nom : '"+destination+"'}}], debut : {$lte : {$date : "+datedebut+"}}, fin : {$gte : {$date : "+c.getTime()+"}}}");

         return mongoTemplate.find(q, Voyage.class);
    }

I have documents on my database, but i don't receive anyone, my list is empty. In addition, i don't catch any MongoException ...

Have you got an solution ?


Solution

  • I came to a compromise, with mixing BasicQuery with Criteria:

    BasicQuery q = new BasicQuery("{$or : [{paysDestination : {$elemMatch : {nom : '"+destination+"'}}},{paysPrincipal : {nom : '"+destination+"'}}]}");
    q.addCriteria(Criteria.where("debut").lte(datedebut).and("fin").gte(c.getTime()));
    

    If you still have any solution for only coding it with BasicQuery, don't hesitate !