Aggregation error from perl driver run_command

179 views
Skip to first unread message

Chris Matta

unread,
Nov 13, 2012, 4:58:21 PM11/13/12
to mongod...@googlegroups.com
I'm trying to implement an aggregation function I've gotten to successfully work in javascript in perl, but I keep getting the error 'exception: wrong type for field (pipeline) 3 != 4'. Any ideas where I could be going wrong?

my $project = Tie::IxHash->new( 
                        "year" => { '$year' => '$datetime'},
                        "month" => {'$month' => '$datetime'},
                        "day" => {'$dayOfMonth' => '$datetime'},
                        "hour" => { '$hour' => '$datetime'},
                        # the minute field is being rounded down to the nearest resolution
                        "minute" => {'$subtract' => (
                                            {'$minute' => '$datetime'},
                                            {'$mod' => ({'$minute' => '$datetime'}, $resolution)}
                                                     )},
                        array_serial => 1,
                        adaptor_id => 1,
                        processor_id => 1,
                        processor_type => 1,
                        metric => 1
                );

    my $group = Tie::IxHash->new(
                        _id => { array_serial => '$array_serial',
                                adaptor_id => '$adaptor_id',
                                processor_id => '$processor_id',
                                processor_type => '$processor_type',
                                year => '$year',
                                month => '$month',
                                day => '$day',
                                hour => '$hour',
                                minute => '$minute'
                            },
                        metric => {
                            '$avg' => '$metric'
                        }
                );

    my @pipeline = ( 
                    {'$match' => $query},
                    {'$project' => $project},
                    { '$group' => $group }
                    );
    print Dumper(\@pipeline);
    my $aggregateCommand = Tie::IxHash->new(aggregate => $collection, pipeline => @pipeline);

    my $result = $db->run_command($aggregateCommand);

    print Dumper($result);

Chris Matta

unread,
Nov 13, 2012, 5:03:20 PM11/13/12
to mongod...@googlegroups.com
Sorry, documents look like this:

{
        "_id" : ObjectId("5097ffa800ba62343500f7ec"),
        "adaptor_id" : "DKA-2NL",
        "processor_id" : NumberLong(5),
        "datetime" : ISODate("2012-11-05T17:25:00Z"),
        "array_serial" : NumberLong(12345),
        "metric" : 26.989082,
        "processor_type" : "DKP"
}

Stephen Lee

unread,
Nov 14, 2012, 1:25:20 PM11/14/12
to mongod...@googlegroups.com
Judging from the C++ source of the BSON implementation in MongoDB, I'd guess that the 'exception: wrong type for field (pipeline) 3 != 4' refers to the parser expecting either an Object (type == 3) and got an Array (type == 4) or the other way around.

Which line of your Perl script throws the error?  If it's the "print Dumper(\@pipeline)" line, could you try "print Dumper-ing" just the $project and $group individually?  I'm guessing that your Mongo shell JSON to Perl translation might have an error, but after a quick glance, I don't quite see it.

Chris Matta

unread,
Nov 14, 2012, 1:43:32 PM11/14/12
to mongod...@googlegroups.com
I just fixed it, it was a combination of things... 

the @pipeline had to be passed in as a reference, and the arrays I was defining in the '$subtract' and '$mod' operations were defined with ( ) instead of [ ];
Reply all
Reply to author
Forward
0 new messages