Question

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 ?

Was it helpful?

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 !

OTHER TIPS

For anyone looking for this: {<dateField>: ISODate("2022-01-24")} works. As does: {<dateField>: ISODate("2022-01-24T13:40:00.000Z")}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top