in reply to mongodb dynamic filter

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.

Replies are listed 'Best First'.
Re^2: mongodb dynamic filter
by actarus2003 (Novice) on Nov 09, 2017 at 15:52 UTC

    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