[英]get answer from mongodb in 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" }
我的要求是獲得定義中的陳述
您遇到的語法錯誤來自perl,與MongoDB沒有關系。 要構建hashref,必須使用以下符號:
{
key => value,
other_key => other_value,
}
不是{ "key": "value" }
(看起來像JSON)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.