PHP8 MongoDB\Driver\Manager::executeCommand

2024-04-08 10:15 更新

(mongoDB >=1.0.0)

MongoDB\Driver\Manager::executeCommand — 執(zhí)行數(shù)據(jù)庫命令

說明

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

根據(jù)選項選擇服務(wù)器 并在該服務(wù)器上執(zhí)行該命令。默認(rèn)情況下,主服務(wù)器將 被選中。"readPreference"

此方法不對命令應(yīng)用任何特殊邏輯。雖然這種方法 接受和選項,它們將被合并到 命令文檔中,這些選項不會默認(rèn)為相應(yīng)的值 從 MongoDB 連接 URI 也不會考慮 MongoDB 服務(wù)器版本 帳戶。因此,鼓勵用戶使用特定的讀取和/或?qū)懭?命令方法(如果可能)。"readConcern""writeConcern"

參數(shù) 

db (字符串)

要在其上執(zhí)行命令的數(shù)據(jù)庫的名稱。

command (MongoDB\驅(qū)動程序\命令)

要執(zhí)行的命令。

options
選項
選擇類型描述
read關(guān)注MongoDB\驅(qū)動程序\ReadConcern

要應(yīng)用于操作的讀取關(guān)注點。

此選項在 MongoDB 3.2+ 中可用,將導(dǎo)致 如果為較舊的服務(wù)器指定了執(zhí)行時的異常 版本。

readPreferenceMongoDB\Driver\ReadPreference

用于為操作選擇服務(wù)器的讀取首選項。

會期MongoDB\驅(qū)動程序\會話

要與操作關(guān)聯(lián)的會話。

寫關(guān)注點MongoDB\驅(qū)動程序\WriteConcern

要應(yīng)用于操作的寫入關(guān)注點。

警告

如果您使用的是具有事務(wù)的 在進(jìn)行中,不能指定 OR 選項。這將導(dǎo)致引發(fā) MongoDB\Driver\Exception\InvalidArgumentException。相反,您應(yīng)該在創(chuàng)建時設(shè)置這兩個選項 使用 MongoDB\Driver\Session::startTransaction() 的事務(wù)。"session""readConcern""writeConcern"

返回值 

成功后返回 MongoDB\Driver\Cursor。

錯誤/異常 

  • 如果該選項與與 or 選項結(jié)合使用的關(guān)聯(lián)事務(wù)一起使用,則引發(fā) MongoDB\Driver\Exception\InvalidArgumentException。"session""readConcern""writeConcern"
  • 如果該選項與未確認(rèn)的寫入問題結(jié)合使用,則引發(fā) MongoDB\Driver\Exception\InvalidArgumentException。"session"
  • 在參數(shù)分析錯誤時拋出 MongoDB\Driver\Exception\InvalidArgumentException。
  • 如果與服務(wù)器的連接失?。ㄓ捎谏矸蒡炞C以外的原因),則拋出 MongoDB\Driver\Exception\ConnectionException。
  • 如果需要身份驗證并失敗,則拋出 MongoDB\Driver\Exception\AuthenticationException。
  • 在其他錯誤(例如,無效命令,向輔助命令發(fā)出寫入命令)時拋出 MongoDB\Driver\Exception\RuntimeException。

更新日志 

版本說明
PECL mongodb 1.4.4MongoDB\Driver\Exception\InvalidArgumentException,如果在 與未確認(rèn)的寫入問題相結(jié)合。"session"
PECL mongodb 1.4.0第三個參數(shù)現(xiàn)在是一個數(shù)組。 為了向后兼容,此參數(shù)仍將接受 MongoDB\Driver\ReadPreference 對象。options

示例 

示例 #1 MongoDB\Driver\Manager::executeCommand() 帶有返回單個結(jié)果文檔的命令

<?php

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

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

/* The ping command returns a single result document, so we need to access the
 * first result in the cursor. */
$response = $cursor->toArray()[0];

var_dump($response);

?>

以上示例會輸出:

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

示例 #2 MongoDB\Driver\Manager::executeCommand() 帶有返回游標(biāo)的命令

<?php

$manager = new MongoDB\Driver\Manager("mongodb://localhost: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 限制命令的執(zhí)行時間

可以通過在 MongoDB\Driver\Command 文檔中指定 for 的值來限制命令的執(zhí)行時間。請注意,這一次 limit 在服務(wù)器端強(qiáng)制執(zhí)行,不會將網(wǎng)絡(luò)延遲考慮在內(nèi) 帳戶。有關(guān)詳細(xì)信息,請參閱 MongoDB 手冊中的 ? 終止正在運(yùn)行的操作。"maxTimeMS"

<?php

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

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

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

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

?>

如果命令在執(zhí)行一秒鐘后無法完成 服務(wù)器,則將拋出 MongoDB\Driver\Exception\ExecutionTimeoutException。

注釋 

注意: 如果使用輔助 readReference,則它是 調(diào)用方負(fù)責(zé)確??梢栽谳o助設(shè)備上執(zhí)行命令。無驗證 由司機(jī)完成。
注意: 此方法不默認(rèn)使用MongoDB連接中的讀取首選項 URI中。需要該行為的應(yīng)用程序應(yīng)考慮使用 MongoDB\Driver\Manager::executeReadCommand()。

參見 

  • MongoDB\驅(qū)動程序\命令
  • MongoDB\驅(qū)動程序\游標(biāo)
  • MongoDB\Driver\Manager::executeReadCommand() - 執(zhí)行讀取
  • MongoDB\Driver\Manager::executeReadWriteCommand() - 執(zhí)行讀取和寫入的數(shù)據(jù)庫命令
  • MongoDB\Driver\Manager::executeWriteCommand() - 執(zhí)行一個數(shù)據(jù)庫命令,該命令將
  • MongoDB\Driver\Server::executeCommand() - 在此服務(wù)器上執(zhí)行數(shù)據(jù)庫命令


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號