PHP Conference Japan 2024

MongoDB\Driver\Manager::executeCommand

(mongodb >=1.0.0)

MongoDB\Driver\Manager::executeCommand執行資料庫命令

說明

final public MongoDB\Driver\Manager::executeCommand(string $db, MongoDB\Driver\Command $command, array|MongoDB\Driver\ReadPreference|null $options = null): MongoDB\Driver\Cursor

根據 "readPreference" 選項選取伺服器,並在該伺服器上執行命令。

此方法不會對命令套用任何特殊邏輯。"readPreference""readConcern""writeConcern" 選項的預設值將從活動交易(由 "session" 選項指示)推斷。如果沒有活動交易,則會使用主要讀取偏好來選取伺服器。

預設值不會連線 URI 推斷。因此,如果可能,建議使用者使用特定的讀取和/或寫入命令方法。

參數

db (string)

要在其上執行命令的資料庫名稱。

command (MongoDB\Driver\Command)

要執行的命令。

options

options
選項 類型 說明
readConcern MongoDB\Driver\ReadConcern

要套用至作業的讀取關注。

此選項在 MongoDB 3.2+ 中可用,如果為較舊的伺服器版本指定,將在執行時產生例外。

readPreference MongoDB\Driver\ReadPreference

用於選取作業伺服器的讀取偏好。

session MongoDB\Driver\Session

要與作業關聯的會話。

writeConcern MongoDB\Driver\WriteConcern

要套用至作業的寫入關注。

警告

如果您正在使用具有進行中交易的 "session",則無法指定 "readConcern""writeConcern" 選項。這將導致擲回 MongoDB\Driver\Exception\InvalidArgumentException。相反地,您應該在您使用 MongoDB\Driver\Session::startTransaction() 建立交易時設定這兩個選項。

回傳值

成功時回傳 MongoDB\Driver\Cursor

錯誤/例外

變更記錄

版本 說明
PECL mongodb 1.4.4 如果 "session" 選項與未確認的寫入關注結合使用,則會擲回 MongoDB\Driver\Exception\InvalidArgumentException
PECL mongodb 1.4.0 第三個參數現在是 options 陣列。為了向後相容,此參數仍然接受 MongoDB\Driver\ReadPreference 物件。

範例

範例 1 MongoDB\Driver\Manager::executeCommand(),使用回傳單一結果文件的命令

<?php

$manager
= new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
$command = new MongoDB\Driver\Command(['ping' => 1]);

try {
$cursor = $manager->executeCommand('admin', $command);
} catch(
MongoDB\Driver\Exception $e) {
echo
$e->getMessage(), "\n";
exit;
}

/* ping 命令回傳單一結果文件,因此我們需要存取
* cursor 中的第一個結果。 */
$response = $cursor->toArray()[0];

var_dump($response);

?>

以上範例將輸出

array(1) {
  ["ok"]=>
  float(1)
}

範例 2 MongoDB\Driver\Manager::executeCommand(),使用回傳 cursor 的命令

<?php

$manager
= new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'y' => 'foo']);
$bulk->insert(['x' => 2, 'y' => 'bar']);
$bulk->insert(['x' => 3, 'y' => 'bar']);
$manager->executeBulkWrite('db.collection', $bulk);

$command = new MongoDB\Driver\Command([
'aggregate' => 'collection',
'pipeline' => [
[
'$group' => ['_id' => '$y', 'sum' => ['$sum' => '$x']]],
],
'cursor' => new stdClass,
]);
$cursor = $manager->executeCommand('db', $command);

/* The aggregate command can optionally return its results in a cursor instead
* of a single result document. In this case, we can iterate on the cursor
* directly to access those results. */
foreach ($cursor as $document) {
var_dump($document);
}

?>

以上範例將輸出

object(stdClass)#6 (2) {
  ["_id"]=>
  string(3) "bar"
  ["sum"]=>
  int(10)
}
object(stdClass)#7 (2) {
  ["_id"]=>
  string(3) "foo"
  ["sum"]=>
  int(2)
}

範例 #3 限制命令的執行時間

可以透過在 MongoDB\Driver\Command 文件中指定 "maxTimeMS" 的值來限制命令的執行時間。請注意,此時間限制是在伺服器端強制執行的,並未將網路延遲納入考量。有關更多資訊,請參閱 MongoDB 手冊中的» 終止執行中的操作

<?php

$manager
= new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');

$command = new MongoDB\Driver\Command([
'count' => 'collection',
'query' => ['x' => ['$gt' => 1]],
'maxTimeMS' => 1000,
]);

$cursor = $manager->executeCommand('db', $command);

var_dump($cursor->toArray()[0]);

?>

如果命令在伺服器上執行一秒後仍未完成,將會拋出 MongoDB\Driver\Exception\ExecutionTimeoutException 異常。

注意事項

注意: 如果使用次要的 readPreference,呼叫者有責任確保 command 可以在次要節點上執行。驅動程式不會執行任何驗證。

注意: 這個方法預設不會使用 MongoDB 連線 URI 中的讀取偏好設定。需要此行為的應用程式應考慮使用 MongoDB\Driver\Manager::executeReadCommand()

參見

新增註解

使用者貢獻的註解

此頁面沒有使用者貢獻的註解。
To Top