1

I use perl script as a client to query mongoDB databases.my code is as below:

my $some_users = $users->find({"name" => "Joe"});
 while (my $doc = $all_users->next) {
        print $doc->{'name'}."\n";
    }

cpan tutorial says:

find returns a MongoDB::Cursor, which can be iterated over. It lazily loads results from the database.

And yes, after my careful observation,I find that it takes a very short time when calling the find() , instead, the iteration of MongoDB::Cursor will take a long time if the find() function returns many data.So ,I becomes very confused,what does it mean by lazy load?Lazy load means "do nothing" ?lazy load means cheating?

1
  • 1
    It's only cheating if you get caught. Commented Oct 8, 2013 at 15:36

1 Answer 1

1

Lazy load means returning an iterator instead of whole data.

So in your loop :

This does not return and store full data in $some_users, only a reference/iterator to the data matching the query.

    my $some_users = $users->find({"name" => "Joe"});

Now you can iterate over that data using the iterator, the records are fetched by the Perl Mongodb driver in the loop, it may fetch them one by one, or prefetch a batch of say 1000 records,and then refetch after 1000, that depends on the driver implementation.

     while (my $doc = $all_users->next) {
            print $doc->{'name'}."\n";
        }
Sign up to request clarification or add additional context in comments.

Comments

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.