MongoDB::Cursor - A cursor/iterator for Mongo query results
version 0.25
while (my $object = $cursor->next) { ... } my @objects = $cursor->all;
$MongoDB::Cursor::slave_okay = 1;
Whether it is okay to run queries on the slave. Defaults to 0.
If this cursor has queried the database yet. Methods mofifying the query will complain if they are called after the database is queried.
$coll->insert({name => "Fred", age => 20}); my $cursor = $coll->query->fields({ name => 1 }); my $obj = $cursor->next; $obj->{name}; "Fred" $obj->{age}; # undef
Selects which fields are returned. The default is all fields. _id is always returned.
# sort by name, descending my $sort = {"name" => -1}; $cursor = $coll->query->sort($sort);
Adds a sort to the query. Argument is either a hash reference or a Tie::IxHash. Returns this cursor for chaining operations.
$per_page = 20; $cursor = $coll->query->limit($per_page);
Returns a maximum of N results. Returns this cursor for chaining operations.
$page_num = 7; $per_page = 100; $cursor = $coll->query->limit($per_page)->skip($page_num * $per_page);
Skips the first N results. Returns this cursor for chaining operations.
my $cursor = $coll->query->snapshot;
Uses snapshot mode for the query. Snapshot mode assures no duplicates are returned, or objects missed, which were present at both the start and end of the query's execution (if an object is new during the query, or deleted during the query, it may or may not be returned, even with snapshot mode). Note that short query responses (less than 1MB) are always effectively snapshotted. Currently, snapshot mode may not be used with sorting or explicit hints.
my $cursor = $coll->query->hint({'x' => 1});
Force Mongo to use a specific index for a query.
my $explanation = $cursor->explain;
This will tell you the type of cursor used, the number of records the DB had to examine as part of this query, the number of records returned by the query, and the time in milliseconds the query took to execute. Requires boolean package.
my $num = $cursor->count;
Returns the number of document this query will return.
Resets the cursor. After being reset, pre-query methods can be called on the cursor (sort, limit, etc.) and subsequent calls to next, has_next, or all will re-query the database.
while ($cursor->has_next) { ... }
Checks if there is another result to fetch.
while (my $object = $cursor->next) { ... }
Returns the next object in the cursor. Will automatically fetch more data from the server if necessary. Returns undef if no more data is available.
my @objects = $cursor->all;
Returns a list of all objects in the result.
Kristina Chodorow <kristina@mongodb.org>
To install MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDB
CPAN shell
perl -MCPAN -e shell install MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.