Search code examples
mongodbmongodb-queryjasper-reports

Generating report with optional params using jasperStudio and mongoDB


I'm trying to generate report using jasperStudio with optional param $P{p_site_id} if param not equal 0 return just matching data else return all data

I make condition on $match as below the code doesn't give me error when running I got exception : fields not found

how I can solve this ? please

{
    runCommand:
    {
        "aggregate" : "device_inventories",
            "pipeline" : [
              { $group: { _id: { deviceStatusCode: "$deviceStatusCode" , siteId : "$siteId"}, count: { $sum: 1 } } }
                    {
                       $match: {
                            $expr: {
                                 $cond: [
                                     { $eq: [ {$ifNull: [ $P{p_site_id},0 ] } , 0 ] },
                                     {$eq:[true,true]},
                                     {$eq:["_id.siteId" , $P{p_site_id}]},
                                 ]
                           }
                      }
                   }
                { $lookup: { from: "device_statuses", localField: "_id.deviceStatusCode", foreignField: "deviceStatusCode", as: "statusInfo" } },
                { $lookup: { from: "sites", localField: "_id.siteId", foreignField: "siteId", as: "sitesInfo" } },
                {
                    $project: {
                        _id: 0,
                        statusInfo: { $first: "$statusInfo.deviceStatus" },
                        sitesInfo: { $first: "$sitesInfo.siteName" },
                        count: 1
                    }
                }
            ],
}
}

Solution

  • There is no clear way to achieve that the best solution to generate report from mongoDB atlas with jasper Studio is to create JDBC connection as mentioned on mongoDB atlas documentation JDBC Connection mongoDB