I need to match documents by date in an aggregation query executed in "strict JSON" mode via db.runCommand(). These requirements are dictated by the way JasperReports communicates with MongoDB
Document schema:
{
"_id" : ObjectId("5a6fca214420c2302a6f8985"),
"report_date" : ISODate("2018-01-23T00:00:00.000+0000"),
...
}
This works in MongoDB console (returns matching documents), but it doesn't work in JasperReports (throwing com.mongodb.util.JSONParseException
in the line containing the "ISODate()" command). I'm showing only the first stage of the pipeline for clarity:
db.runCommand(
{
"aggregate" : "reports",
"pipeline" : [
{
"$match": {
"report_date": {
"$gte": ISODate("2017-01-23")
}
}
}
],
"cursor": {}
})
The same query using $date
executes well in both MongoDB and JasperReports, but does not return any documents (0 matches in both scenarios):
db.runCommand(
{
"aggregate" : "reports",
"pipeline" : [
{
"$match": {
"report_date": {
"$gte": {"$date": "2017-01-23T00:00:00.000Z"}
}
}
}
],
"cursor": {}
})
Here goes a response example for the second query:
{
"cursor" : {
"firstBatch" : [ ],
"id" : NumberLong(0),
"ns" : "reports"
},
"ok" : 1
}
I observe the same behavior in range queries, like in the following snippet:
...
"$gte": {"$date": "2017-01-29T00:00:00.000Z"},
"$lt": {"$date": "2019-01-29T00:00:00.000Z"}
...
I also tried using $dateFromString
instead of $date
(interestingly, using this one in MongoDB 3.2 didn't result in any exceptions being thrown) - same result.
How can I run queries by date or date ranges in the "match" stage of aggregation pipeline and strict-JSON mode?
Tested versions: MongoDB 3.2.12 and 3.6.2, JasperReports Studio: 6.5.1 final
For any suggestions thanks in advance!