Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example use of Perl MongoDB::Collection aggregate function

Can anyone please share an example of proper use of the MongoDB::Collection aggregate function in Perl? I am looking to do a simple sum of a field where another field equals some value x.

From there I'll be able to figure out the rest.

Thanks

like image 650
MadHacker Avatar asked May 21 '26 22:05

MadHacker


1 Answers

Here is an example which I've built.

for (my $i=0; $i<100; $i++) {
    my $number = $i*100;
    my $result = $collection->aggregate([{"\$match" => {"b" => {"\$gte" => $number, "\$lt" => $number+1000}}}, {"\$group" => {"_id" => 0, "average" => {"\$avg" => "\$b"}, "count" => {"\$sum" => 1}}}]);
    print Dumper($result);
}

You can find more information at http://docs.mongodb.org/manual/aggregation/.

like image 82
GRMrGecko Avatar answered May 23 '26 16:05

GRMrGecko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!