actarus2003 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I'm playing with mongodb and i need to apply filters from dancer2 query_parameters, but i fail to construct the filter object.

I had try to create a base object like

my %serchstrx={'$and' => []};

and then pushing the pair (key&value) in the array like

for(my $id=0;$id < keys %hfields;$id++){ if(query_parameters->get("columns[$id][searchable]") eq 'true' && defined query_parameters->get("columns[$id][search][value]") && query_parameters->get("columns[$id][search][value]") ne ''){ push @{$serchstrx{'$and'}},'{"'. query_parameters->get("column +s[$id][data]") . '" => qr/' . query_parameters->get("columns[$id][sea +rch][value]").'/i}'; } }

%hfield contain the name of possible key name in the form

but, it never work for me

Any suggestione ?

Replies are listed 'Best First'.
Re: mongodb dynamic filter
by roboticus (Chancellor) on Nov 06, 2017 at 21:04 UTC

    actarus2003:

    That's a rather convoluted chunk of code.

    I'd suggest putting "use strict; use warnings;" at the top of your script. That way, the line:

    my %serchstrx={'$and' => []};

    would give you a warning that you could fix. I'd also suggest dumping your structure, something like:

    use Data::Dumper; ... your code ... print "Search: ", Dumper(\%serchstrx),"\n";

    So you can see what it is you're actually building and trying to pass on to MongoDB.

    It looks like there's plenty of other things to look at, but I'd start with those first.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      First of all thanks to all Monks suggested me the way.

      Then i take my dirty own way using yours suggestions ...

      my @serchstra; my $ccount=0; for(my $id=0;$id < keys %hfields;$id++){ info("compose search $id ",query_parameters->get("columns[$id][sea +rchable]"),' >',query_parameters->get("columns[$id][search][value]"), +'< ',query_parameters->get("columns[$id][data]")); if(query_parameters->get("columns[$id][searchable]") eq 'true' && defined query_parameters->get("columns[$id][search][value]") & +& query_parameters->get("columns[$id][search][value]") ne '') { my $kvalue=query_parameters->get("columns[$id][search][value]" +); push @serchstra,{ query_parameters->get("columns[$id][data]") + => qr/$kvalue/i}; $ccount++; } } push @{$serchstr{q{$and}}},@serchstra; info("HH0 serchstr is:".Dumper(%serchstr) ."\n---- lengh of serchstra +is: " .scalar @serchstra . " but ccount $ccount"); $serchstr = \%serchstr if($ccount > 0); @ret = $collection->find($serchstr)->fields(\%hfields)->limit(params-> +{length})->skip(params->{start})->all; }

      This piece of code work for me, I may have a variabile number of search parameters from datables.net (so i can reuse with different table with different number of columns and different names of columns), without work/hack the datatables callback (not sure can be done in the search).

      Now my table with data from mongo, displayed with datatables (server side processing option) and dancer2 can be also filtered foreach columns.

      Thanks for yours precious helps.

      Stefano

Re: mongodb dynamic filter
by 1nickt (Canon) on Nov 06, 2017 at 23:00 UTC

    Any suggestions ?

    Use whitespace. They don't charge for it ;-)

    Also, don't try to use query parameters but rather POST a data structure, e.g. in JSON, using Dancer2's serialization to get at the data.

    For better answers, please describe what "it never work for me" means. See the FAQ Posting on PerlMonks.


    The way forward always starts with a minimal test.

      Unfortunately the post data come from datatables.net js table manager library, i can't easy change it.

      When i said it never work for me i mean mongo library fail to use my filter, usually i got an exception in dancer2.

      I will play with suggestion i had receive now.

        Hi again,

        Please describe what "it never work for me" means.
        When i said it never work for me i mean mongo library fail to use my filter, usually i got an exception in dancer2

        Unfortunately this is not very much more useful than the first statement you made. In order to be useful information for anyone (including yourself) who is trying to debug the issue, you would better describe it something like:

        When I pass the constructed filter string to MongoDB using DBI (or Dancer2::Plugin::Database, or whatever you are in fact using), I get a fatal exception, noted in the Dancer2 error log as:
        Full text of the error from the log

        The error message is, after all, trying to tell you what's going on.

        ( Regarding the fact that DataTables is making the query to your Dancer2 app ... Here is not the place to debug the Jquery part of your project, but I'll just say that if the built-in DataTables functionality does not fit (e.g. if you are using Editor and "cannot easily" change the query), it's not difficult to create a callback on a row or cell and regain complete control over the requests made to the server. )


        The way forward always starts with a minimal test.
Re: mongodb dynamic filter
by poj (Abbot) on Nov 06, 2017 at 21:04 UTC
    Any suggestione ?
    for my $id ( 0 .. (keys %hfields)-1 ){ my $searchable = query_parameters->get("columns[$id][searchable]"); if ($searchable eq 'true' ){ my $value = query_parameters->get("columns[$id][search][value]"); if ((defined $value) && ($value ne '')){ my $data = query_parameters->get("columns[$id][search][data]"); push @{$serchstrx{'$and'}},{ $data => qr/$value/i }; } } }
    poj