用perl从mongodb获取答案
我已经用Perl编写了从MongoDB获取答案的代码,但它不起作用。它总是给出上下文错误。我试图做的是根据MongoDB中的搜索条件获取结果用perl从mongodb获取答案,mongodb,perl,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
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" }
我的要求是在define中获取语句,即我有您得到的语法错误来自perl,与MongoDB无关。 要构建hashref,必须使用以下符号:
{
key => value,
other_key => other_value,
}
not
{“key”:“value”}
(看起来像JSON)。您得到的语法错误来自perl,与MongoDB无关。
要构建hashref,必须使用以下符号:
{
key => value,
other_key => other_value,
}
not{“key”:“value”}
(看起来像JSON)