MongoDB\Driver\Query
PHP Manual

MongoDB\Driver\Query::__construct

(mongodb >=1.0.0)

MongoDB\Driver\Query::__constructConstruct new Query

Description

final public MongoDB\Driver\Query::__construct ( array|object $filter [, array $queryOptions ] )

Parameters

filter (array|object)

The search filter.

queryOptions

queryOptions
Option Type Description
limit integer The number of documents to be returned
batchSize integer The number of documents to return per batch
skip integer The number of documents to skip before returning
sort array|object The order in which to return matching documents
modifiers array Meta operators modifying the output or behavior of a query
projection array|object Specifies the fields to return using booleans or projection operators
tailable bool Cursor will not be closed when the last data is retrieved. You can resume this cursor later
slaveOk bool Allow query of replica set secondaries
oplogReplay bool Internal MongoDB Server flag
noCursorTimeout bool Do not timeout a cursor that has been idle for more then 10minutes
awaitData bool Block rather than returning no data. After a period, time out. Useful for tailable cursor
exhaust bool Stream the data down full blast in multiple "reply" packets. Faster when you are pulling down a lot of data and you know you want to retrieve it all
partial bool Get partial results from mongos if some shards are down (instead of throwing an error)

Errors/Exceptions

Examples

Example #1 MongoDB\Driver\Query::__construct() example

<?php
$filter 
= array();
$options = array(
    
/* Only return the following fields in the matching documents */
    
"projection" => array(
        
"title" => 1,
        
"article" => 1,
    ),
    
"sort" => array(
        
"views" => -1,
    ),
    
"modifiers" => array(
        
'$comment'   => "This is a query comment",
        
'$maxTimeMS' => 100,
    ),
);

$query = new MongoDB\Driver\Query($filter$options);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$cursor $manager->executeQuery("databaseName.collectionName"$query$readPreference);

foreach(
$cursor as $document) {
    
var_dump($document);
}

?>

See Also


MongoDB\Driver\Query
PHP Manual