尝试在Perl中使用MongoDB的游标排序时出错

时间:2018-06-14 12:07:40

标签: mongodb perl

This documentation描述了在获得光标后如何对结果进行排序。

但是,当我尝试使用自己的实现进行编译时:

#!/asp_share/linopt/perl-5.8.8/bin/perl

use CGI;
use Fcntl;
use MongoDB;
use JSON;
use Text::CSV;
use strict;
use warnings;

my $q = new CGI;
my $dbhost = $q->param('dbhost') || 'sb2mdb01';
print $q->header('application/json');

# Connect to db and retrieve stats for given db
my $client = MongoDB::Connection->new("host" => $dbhost);
my $tracking_db = $client->get_database("db_size_tracking");
my $db_name = "st_" . ($q->param('db') || "db_size_tracking" );
my $collection = $tracking_db->get_collection($db_name);
my $every = $q->param('every') || 1;  # sample per $every line
my $date = $q->param('date') || `date +%Y%m%d`; # date to start from
chomp($date);
my $year = substr($date,0,4);
my $month = "".substr($date,4,2);
my $day = "".substr($date,6,2);
my $cursor = $collection->find({recorded_at => {'$gte' => DateTime->new( year => $year, month => $month, day => $day)}});
$cursor->sort([recorded_at => 1]);
# Generate and return JSON
my @sizeTimes;
my $count = 0;
while (my $doc = $cursor->next) {
    next if ($count++ % $every != 0);
    my $recorded_at = $doc->{'recorded_at'}->iso8601;
    my $datasize = $doc->{'datasize'};
    my %row;
    $row{'recorded_at'} = $recorded_at;
    $row{'datasize'} = $datasize;
    push @sizeTimes, \%row;
}

print encode_json {sizeTimes => \@sizeTimes};

我得到了

not a hash reference at /linopt/perl-5.8.8/lib/site_perl/5.8.8/i686-linux/MongoDB/Cursor.pm line 182
        MongoDB::Cursor::sort('MongoDB::Cursor=HASH(0x8e87160)', 'ARRAY(0x8e86614)')

我对perl很新,所以我不确定如何解释这个错误信息,任何帮助都会非常感激!

返回的文件格式为

{recorded_at: recorded_at, datasize: datasize}

Perl:v5.8.8 MongoDB:3.0.7

1 个答案:

答案 0 :(得分:1)

front end application               back end application
 launched by user         --/-->   launched with a sytem user
  local machine                     local or remote machine

也可用于按multiple fields.

排序

该文档适用于perl的较新的MongoDB驱动程序,我敢打赌,如果你也在查看它,你就会被困住。