Execute a MongoDB query


(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeQueryExecute a MongoDB query

Описание

final public MongoDB\Driver\Result MongoDB\Driver\Manager::executeQuery ( string $namespace , MongoDB\Driver\Query $query [, MongoDB\Driver\ReadPreference $readPreference ] )

Список параметров

namespace

A fully qualified namespace (databaseName.collectionName)

query

A MongoDB\Driver\Query to execute.

readPreference

Optionally, a MongoDB\Driver\ReadPreference to route the command to. If none given, defaults to the Read Preferences set by the MongoDB Connection URI.

Возвращаемые значения

Returns MongoDB\Driver\Result on success, throws exception (instanceof MongoDB\Driver\Exception) on failure.

Ошибки

Примеры

Пример #1 MongoDB\Driver\Manager::executeQuery() example

<?php
$filter 
= array(
    
"tag" => "mongodb",
    
"views" => array('$gt' => 5),
);
$options = array(
    
"projection" => array(
        
"title" => 1,
        
"article" => 1,
    ),
    
"sort" => array(
        
"views" => -1,
    ),
);
$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$query = new MongoDB\Driver\Query($filter$options);

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

foreach(
$result as $document) {
    echo 
$document["title"], "\n";
}

?>