mongodbmongodb-queryjongo

How to run query with Jongo to get records between given days?


Using Jongo API to query MongoDB, I can fetch documents from last 5 days with

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
employees.find(
  "{ createdOn: { $gt:  # } }", 
  cal.getTimeInMillis() - 5 * 24 * 60 * 60 * 1000
); 

I would like to know how to query records in time interval (5 days from now, 3 days from now).

Assuming today is Aug 9, I need records from 4th to 6th Aug.

Thanks.


Solution

  • According to documentation, with Jongo API you can pass multiple parameters to your query as

    employees.find(
      "{ createdOn: { $gt: #, $lt: #  } }",
      nowInMs - 5 * 24 * 60 * 60 * 1000, 
      nowInMs - 3 * 24 * 60 * 60 * 1000
    );
    

    that will find documents with createdOn field in time interval (5 days from now; 3 days from now).