在perl中从mongodb获取答案

时间:2018-07-09 08:38:50

标签: mongodb perl

我已经写了代码从Perl的MongoDB中获取答案,但是它不起作用。它总是给上下文错误。我想做的是根据MongoDB中的搜索条件获取结果

use strict;
use warnings;
use MongoDB;
use Data::Dumper;

sub answer_get {
my $database = shift;
my $database_collection = shift;
my $msg = shift;
my $client = MongoDB->connect();
my $db = $client->get_database( 'tutorial' );
my $query_result = $db->get_collection( 'users' )->find({context: "what", 
keyword: "IP"},{"define":1});
}
my $get_answer = answer_get("tutorial", "users", "IP");
#print $get_answer;

输出

syntax error at mongoreply.pl line 15, near "context:"
Global symbol "$query_result" requires explicit package name (did you forget 
to declare "my $query_result"?) at mongoreply.pl line 21.
syntax error at mongoreply.pl line 25, near "}"
Execution of mongoreply.pl aborted due to compilation errors.

在MongoDB中运行它时,我会得到答案

db.users.find({context: "what", keyword: "IP"},{"define":1})
{ "_id" : ObjectId("5b4314bda763102504004ea1"), "define" : "An Internet 
Protocol address (IP address) is a numerical label assigned to each device 
connected to a computer network that uses the Internet Protocol for 
communication" }

我的要求是在定义中声明自己拥有

1 个答案:

答案 0 :(得分:2)

您遇到的语法错误来自perl,与MongoDB无关。 要构建hashref,您必须使用以下表示法:

{ "key": "value" }

不是io.out0 := io.out (i+4, i) (看起来像JSON)。