6

I read http://api.mongodb.org/perl/current/MongoDB/Examples.html and seems to be only documentation from mongoDB on Perl. How do I get my query result from mongoDB in perl. Let us say into Hash. I have successfully made connection to the db so far. I managed to do inserts into collections. Now how do I issue select query and get its returned data into hash or something similar?

Update:

Example of my data
{
 "_id" : ObjectId("asdhgajsdghajgh"),
 "country" : "USA"
 "city" : "Boston"
}

{
 "_id" : ObjectId("asdhgajsdghajgh"),
 "country" : "USA"
 "city" : "Seattle"
}

{
 "_id" : ObjectId("asdhgajsdghajgh"),
 "country" : "Canada"
 "city" : "Calgary"
}

My code

my $cursor = $my_collection
    ->find({ country => 1 }) 
    ;
while (my $row = $cursor->next) {
    print "$row\n";
}

This code is not resulting any output. I want to basically iterate through the entire collection and read document by document. Not sure what I am doing wrong. I used the code above. I changed $cur->next to $cursor->next I am guessing it was a typo. I appreciate all the answers so far.

1
  • 1
    The stuff on api.mongodb.org is out of date. You should look at CPAN. I've updated the Perl link on api.mongodb.org to point there. Commented Jan 16, 2013 at 1:35

1 Answer 1

10

That's not the official documentation. Head right to the CPAN:

Iterating results is pretty similar to the DBI way:

use Data::Printer;
use MongoDB;

# no query is performed on initialization!
my $cursor = $collection
    ->find({ active => 1, country => 'Canada' }) # filter "active" records from Canada
    ->sort({ stamp => -1 }) # order by "stamp" attribute, desc.
    ->limit(1000);          # first 1000 records

# query & iterate
while (my $row = $cur->next) {
    # it is 'p', from Data::Printer!
    p $row;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply. I read some of the documentation above and it helped me to understand how to get the result which is pretty straight forward like my $some_users = $users->find({"name" => "Joe"}); This example does not explain what happens when find() returns millions of results? How do I iterate through those results?
Sorry, added the iterator example!
thanks guys for your answers. I updated my question again. Can you guys check and see why my code is not resulting in any output. I am guessing it is a very simple noob mistake.
Edited the answer! Oops, it's not print, it's just p! You have to install Data::Printer, though. Also, country => 1 will search for country with numeric value of 1. It won't return anything in your case.
quick reply Thanks. I found out what I was looking for finally. my $cursor = $my_collection->find;while (my $row = $cursor->next) {print $row->{'country'}} This worked

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.