update : 2015.11.03
php.shukuma.com

검색:
 
 
Execute a MongoDB query

MongoDB\Driver\Manager::executeQuery

(mongodb >=0.2.0)

MongoDB\Driver\Manager::executeQueryExecute a MongoDB query

설명

final public MongoDB\Driver\Cursor 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\Cursor on success, throws exception (instanceof MongoDB\Driver\Exception) on failure.

오류/예외

  • Throws MongoDB\Driver\AuthenticationException if authentication is needed and fails
  • Throws MongoDB\Driver\ConnectionException if connection to the server fails for other then authentication reasons
  • Throws MongoDB\Driver\RuntimeException on other errors, such as invalid queries

예제

Example #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");
$cursor $manager->executeQuery("databaseName.collectionName"$query$readPreference);

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

?>