class Query implements IteratorAggregate (View source)

Query class used in conjunction with the Builder class for executing queries or commands and returning results.

Constants

TYPE_FIND

TYPE_FIND_AND_UPDATE

TYPE_FIND_AND_REMOVE

TYPE_INSERT

TYPE_UPDATE

TYPE_REMOVE

TYPE_GROUP

TYPE_MAP_REDUCE

TYPE_DISTINCT

TYPE_GEO_NEAR

TYPE_COUNT

TYPE_GEO_LOCATION

Properties

protected Collection $collection The Collection instance.
protected array $query Query structure generated by the Builder class.
protected Iterator $iterator
protected array $options Query options

Methods

__construct(Collection $collection, array $query, array $options)

Constructor.

integer
count(boolean $foundOnly = false)

Count the number of results for this query.

mixed
debug(string $name = null)

Return an array of information about the query structure for debugging.

mixed
execute()

Execute the query and return its result.

getIterator()

Execute the query and return its result, which must be an Iterator.

array
getQuery()

Return the query structure.

array|object|null
getSingleResult()

Execute the query and return the first result.

integer
getType()

Return the query type.

iterate() deprecated

Alias of {@link Query::getIterator()}.

array
toArray()

Execute the query and return its results as an array.

prepareCursor(Cursor $cursor)

Prepare the Cursor returned by {@link Query::execute()}.

Details

__construct(Collection $collection, array $query, array $options)

Constructor.

Parameters

Collection $collection
array $query
array $options

Exceptions

InvalidArgumentException if query type is invalid

integer count(boolean $foundOnly = false)

Count the number of results for this query.

If the query resulted in a Cursor, the $foundOnly parameter will ignore limit/skip values if false (the default). If the Query resulted in an EagerCursor or ArrayIterator, the $foundOnly parameter has no effect.

Parameters

boolean $foundOnly

Return Value

integer

mixed debug(string $name = null)

Return an array of information about the query structure for debugging.

The $name parameter may be used to return a specific key from the internal $query array property. If omitted, the entire array will be returned.

Parameters

string $name

Return Value

mixed

mixed execute()

Execute the query and return its result.

The return value will vary based on the query type. Commands with results (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other commands and operations may return a status array or a boolean, depending on the driver's write concern. Queries and some mapReduce commands will return a CursorInterface.

Return Value

mixed

Iterator getIterator()

Execute the query and return its result, which must be an Iterator.

If the query type is not expected to return an Iterator, BadMethodCallException will be thrown before executing the query. Otherwise, the query will be executed and UnexpectedValueException will be thrown if {@link Query::execute()} does not return an Iterator.

Return Value

Iterator

Exceptions

BadMethodCallException if the query type would not return an Iterator
UnexpectedValueException if the query did not return an Iterator

See also

http://php.net/manual/en/iteratoraggregate.getiterator.php

array getQuery()

Return the query structure.

Return Value

array

array|object|null getSingleResult()

Execute the query and return the first result.

Return Value

array|object|null

See also

IteratorAggregate::getSingleResult

integer getType()

Return the query type.

Return Value

integer

Iterator iterate() deprecated

deprecated 1.1 Use {@link Query::getIterator()}; will be removed for 2.0

Alias of {@link Query::getIterator()}.

Return Value

Iterator

array toArray()

Execute the query and return its results as an array.

Return Value

array

See also

IteratorAggregate::toArray

protected CursorInterface prepareCursor(Cursor $cursor)

Prepare the Cursor returned by {@link Query::execute()}.

This method will apply cursor options present in the query structure array. The Cursor may also be wrapped with an EagerCursor.

Parameters

Cursor $cursor

Return Value

CursorInterface