NAME

MongoDB::QueryResult - An iterator for Mongo query results

VERSION

version v2.2.0

SYNOPSIS

$cursor = $coll->find( $filter );
$result = $cursor->result;

while ( $doc = $result->next ) {
    process_doc($doc)
}

DESCRIPTION

This class defines an iterator against a query result. It automatically fetches additional results from the originating mongod/mongos server on demand.

For backwards compatibility reasons, MongoDB::Cursor encapsulates query parameters and generates a MongoDB::QueryResult object on demand. All iterators on MongoDB::Cursor delegate to MongoDB::QueryResult object.

Retrieving this object and iterating on it directly will be slightly more efficient.

USAGE

Error handling

Unless otherwise explicitly documented, all methods throw exceptions if an error occurs. The error types are documented in MongoDB::Error.

To catch and handle errors, the Try::Tiny and Safe::Isa modules are recommended:

Cursor destruction

When a MongoDB::QueryResult object is destroyed, a cursor termination request will be sent to the originating server to free server resources.

Multithreading

NOTE: Per threads documentation, use of Perl threads is discouraged by the maintainers of Perl and the MongoDB Perl driver does not test or provide support for use with threads.

Iterators are cloned in threads, but not reset. Iterating from multiple threads will give unpredictable results. Only iterate from a single thread.

METHODS

has_next

if ( $response->has_next ) {
    ...
}

Returns true if additional documents are available. This will attempt to get another batch of documents from the server if necessary.

next

while ( $doc = $result->next ) {
    process_doc($doc)
}

Returns the next document or undef if the server cursor is exhausted.

batch

while ( @batch = $result->batch ) {
  for $doc ( @batch ) {
    process_doc($doc);
  }
}

Returns the next batch of documents or an empty list if the server cursor is exhausted.

all

@docs = $result->all;

Returns all documents as a list.

AUTHORS

  • David Golden <david@mongodb.com>

  • Rassi <rassi@mongodb.com>

  • Mike Friedman <friedo@friedo.com>

  • Kristina Chodorow <k.chodorow@gmail.com>

  • Florian Ragwitz <rafl@debian.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2019 by MongoDB, Inc.

This is free software, licensed under:

The Apache License, Version 2.0, January 2004