Question: How to use @Query annotation for MongoDB for fetching documents between dates?

I am using @Query annotation for getting documents between specified dates.

@Query("{'admission_date': {$gte: ?0, $lte: ?1}}")
public List<Patient> findByAdmissionDateBetween(String minDate, String maxDate)

Sample Document:

_id: 1015
patientName: "Hemant Srivastav"
gender: "male"
date_of_birth: 2006-07-18T18:30:00.000+00:00
admission_date: 2022-07-01T18:30:00.000+00:00
diagnosis: "broken arm"

but I am getting an exception:

{
  "errorMessage": "Patients between dates not found.",
  "errorCode": 400
}

My Service and Controller method implementation is this.
Controller:

@GetMapping("/admissiondates")
public ResponseEntity<List<Patient>> getPatientsBtwnAdmissionDates(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) String minDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) String maxDate)
            throws PatientAdmissionException {
List<Patient>patientsBtwnDatesList = patientService.getPatientsBtwnAdmissionDates(minDate,maxDate);
return new ResponseEntity<>(patientsBtwnDatesList, HttpStatus.OK);
}

Service:

public List<Patient> getPatientsBtwnAdmissionDates(String minDate, String maxDate)
            throws PatientAdmissionException {
List<Patient> patientBtwnDatesList = patientRepository.getPatientsBtwnAdmissionDates(minDate, maxDate);
if (patientBtwnDatesList.isEmpty() == true) {
    throw new PatientAdmissionException("Patients between dates not found.");
}
return patientBtwnDatesList;
}

I am not able to understand what mistake am I making pls HELP!

Hi @Abhishek_Mathur and welcome to the MongoDB community forum!!

From the above statement, it seems you are trying to compare the ISODate for the date stored in document to the String in the parameter of the function.
Could you confirm that my understanding is correct and the datatype in the documents are string instead of ISODate? You may be able to use Compass Schema analyzer for this

If my understanding is correct, the MongoDB documentation states:

For most data types, comparison operators only perform comparisons on fields where the BSON type matches the query value’s type. MongoDB supports limited cross-BSON comparison through Type Bracketing.

The recommendation here would be to convert the minDate and maxDate to ISO date format and then make the comparison.

Best Regards
Aasawari