1

I am trying to get data from mongodb using perl, but I get undefined value for variable $people

my $client = MongoDB::MongoClient->new(host => 
'mongodb://xxx.xxx.xxx.xxx',port=>27017, username => 'xxxx', 
password => 'xxxx');
my $db = $client->get_database("xxx");
my $collection = $db->get_collection("xxx");
my $people = $collection->find_one({"transactionid" => $id});
while (my $p = $people->next) {
    print Dumper $p;
} 

and I want to get this data :

{
    "_id" : ObjectId("5c453500e2fb4adc98e9fa84"),
    "transactionid" : NumberLong(45282),
    "transactionbillerid" : NumberLong(43137),
    "requesttime" : ISODate("2019-01-21T02:57:04.923Z"),
    "requestmessage" : "xxxxxxxx",
    "responsetime" : ISODate("2019-01-21T02:57:05.236Z"),
    "responsemessage" : "xxx"
}

any suggestions, is there something wrong with my code ?

4
  • Most likely, $id does not have the value you think it has, or there is no element in your collection with the transactionid you're looking for, Commented Jan 27, 2019 at 14:32
  • I get $id value from param.. my $id = $query->param('trx_id'); and there is element in my collection with transactionid I'm looking for :D Commented Jan 27, 2019 at 14:35
  • Does the query work with $id set to a hardcoded value? Try my $id = '1234'; (or whatever the value is) to see if it works. Also, ->find_one does not return an iterator but the document itself. Commented Jan 27, 2019 at 14:55
  • still the same and I just changed my code... XD Commented Jan 27, 2019 at 15:14

1 Answer 1

1

I think you're misunderstanding the value returned by find_one(). There's a big clue in the name, but find_one() returns a single record, not an iterator.

Obviously, I don't have access to your data, so I can't confirm this, but I expect you'll get what you want by running this code:

my $client = MongoDB::MongoClient->new(
  host     => 'mongodb://xxx.xxx.xxx.xxx',
  port     => 27017,
  username => 'xxxx', 
  password => 'xxxx',
);

my $db = $client->get_database("xxx");
my $collection = $db->get_collection("xxx");

my $person = $collection->find_one({"transactionid" => $id});
print Dumper $person;
Sign up to request clarification or add additional context in comments.

7 Comments

I got this response $VAR1 = undef; .-. actually I use this for cgi-perl..
@YudistiraSugandi check your query. In the code you provided there's no a value assignment to $id.. is it a typo error? BTW the answer of @davecross is correct.
I got it from my $id = $query->param('trx_id'); , and I don't know why I got undef value...
and I got new error like this MongoDB::DecodingError: Invalid UTF-8 detected while decoding BSON at /usr/lib/x86_64-linux-gnu/perl5/5.24/MongoDB/BSON.pm
$query->param smells of a query string parameter got using the CGI module. Well.. I guess the problem is in the input data at HTTP layer, so focus your debug on that direction. Check the data received by your web request handler, and why the trx_id parameter is empty.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.