W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
(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"
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í)行時的異常 版本。 |
readPreference | MongoDB\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。
版本 | 說明 |
---|---|
PECL mongodb 1.4.4 | MongoDB\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()。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: