How to make a Dynamic/Optional Filter(parameters) in mongo DB query at (Jasper Studio)

I'm creating a web aplication and it's working perfectly, but at the end user need to create a report from it's data.
On the report page I created some txt boxes where users will type for filtering. Those txt boxes could be empty and I need to return everything from the DB, or some parameter could be filled. Remenbering that I need to pass txt boxes content as params to JasperServer and they will be used in the Query

A example of data input is:

 txtName= empty (NULL), 
    txtCity= 'Belo Horizonte'

I made it in SQL and works perfect. After I tried to use the same logic on mongo but it doesn't work.
But when I do in mongodb the parameter does not return me the desired value, it always returns everything.


SELECT * FROM myfirstreports 
WHERE ($P{city} IS NULL OR cidade =$P{city}) 
AND ($P{name} IS NULL OR nome =$P{name}

The SQL code works perfectly. The code in mongo already does not work.


    'collectionName' : 'myfirstreports',
   'findFields' : 
        'nome': 1, 'numeros': 1, 'vulgo': 1, 'cidade': 1, 
        'usuResponsavelCadastro': 1, 'created_at': 1
    findQuery  : 
        $and: [ 
            {$or:[{ $P{city}: {'$eq': NULL}}, {'cidade': $P{city}}]}, 
            { $or:[{$P{name}: {'$eq': NULL}}, {'nome': $P{name}}]}

Does anybody know how to solve this?

phssilva.estagio's picture
Joined: Jan 21 2019 - 5:17am
Last seen: 3 years 10 months ago

0 Answers:

No answers yet