Use ISODate parameter in a MongoDB query for a report

0

Hi,

I have a Jasper Report with a couple of date parameters for from and to dates.

<parameter name="fromDate" class="java.util.Date" />
<parameter name="toDate" class="java.util.Date" />

I am wanting to use these in a date comparison against a createdDate field that is stored in my MongoDB database as an ISODate

$match : {
  "createdDate": {
    $gte: "$P!{fromDate}",
        $lt: "$P!{toDate}"
  }
}

This doesn't work in terms of not returning any results so I created a variable to convert the date parameter to the ISODate format:

<variable name="formattedFromDate" class="java.lang.String">
  <initialValueExpression><![CDATA[DATEFORMAT($P{fromDate}, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")]]></initialValueExpression>
</variable>

I then changed the $match stage of the pipeline to:

$match : {
  "createdDate": {
    $gte: ISODate("$V!{formattedFromDate}")
  }
}

Again this didn't return any results when it should have but did when I hard-coded it to the value I was setting for the report as a parameter:

$match : {
  "createdDate": {
    $gte: ISODate("2020-01-01T00:00:00.000Z")
  }
}

Any ideas what I'm doing wrong? Unfortunately I can't keep the from and to dates as hard-coded values as the user needs to be able to select these.

sean.1.barry's picture
Joined: Mar 23 2020 - 5:39am
Last seen: 2 months 3 weeks ago

0 Answers:

No answers yet
Feedback
randomness