Jump to content
We've recently updated our Privacy Statement, available here ×

Use ISODate parameter in a MongoDB query for a report


sean.1.barry

Recommended Posts

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.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...