PHP Conference Japan 2024

MongoDB\Driver\Command::__construct

(mongodb >=1.0.0)

MongoDB\Driver\Command::__construct建立新的命令

說明

final public MongoDB\Driver\Command::__construct(陣列|物件 $document, ?陣列 $commandOptions = null)

建構一個新的 MongoDB\Driver\Command,它是一個不可變的值物件,表示一個資料庫命令。然後可以使用 MongoDB\Driver\Manager::executeCommand() 執行該命令。

完整的指令文件,包含指令名稱及其選項,應在 document 參數中表示。commandOptions 參數僅用於指定與指令執行和產生的 MongoDB\Driver\Cursor 相關的選項。

參數

document

將發送到伺服器的完整指令文件。

commandOptions

注意 不要使用此參數來指定 MongoDB 手冊中指令參考中描述的選項。此參數應僅用於下方明確列出的選項。

commandOptions
選項 類型 說明
maxAwaitTimeMS int

正整數,表示伺服器在沒有可用數據時阻塞 getMore 操作的時間限制(以毫秒為單位)。此選項應僅與返回 tailable 游標的指令一起使用(例如 » 變更串流)。

錯誤/例外

更新日誌

版本 說明
PECL mongodb 1.4.0

新增了第二個 commandOptions 參數,支援 "maxAwaitTimeMS" 選項。

範例

範例 #1 MongoDB\Driver\Command::__construct() 範例

<?php

$manager
= new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$command = new MongoDB\Driver\Command(array("buildinfo" => 1));

try {
$cursor = $manager->executeCommand("admin", $command);
$response = $cursor->toArray()[0];
} catch(
MongoDB\Driver\Exception $e) {
echo
$e->getMessage(), "\n";
exit;
}
var_dump($response);

?>

上述範例將輸出類似以下的內容

array(13) {
  ["version"]=>
  string(14) "2.8.0-rc2-pre-"
  ["gitVersion"]=>
  string(62) "b743d7158f7642f4da6b7eac8320374b3b88dc2e modules: subscription"
  ["OpenSSLVersion"]=>
  string(25) "OpenSSL 1.0.1f 6 Jan 2014"
  ["sysInfo"]=>
  string(104) "Linux infant 3.16.0-24-generic #32-Ubuntu SMP Tue Oct 28 13:07:32 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49"
  ["loaderFlags"]=>
  string(91) "-fPIC -pthread -Wl,-z,now -rdynamic -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-E"
  ["compilerFlags"]=>
  string(301) "-Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -fno-builtin-memcmp -std=c99"
  ["allocator"]=>
  string(8) "tcmalloc"
  ["versionArray"]=>
  array(4) {
    [0]=>
    int(2)
    [1]=>
    int(8)
    [2]=>
    int(0)
    [3]=>
    int(-8)
  }
  ["javascriptEngine"]=>
  string(2) "V8"
  ["bits"]=>
  int(64)
  ["debug"]=>
  bool(false)
  ["maxBsonObjectSize"]=>
  int(16777216)
  ["ok"]=>
  float(1)
}

範例 #2 MongoDB\Driver\Command::__construct() 範例

指令也可以接受選項,作為您建立要發送到伺服器的正常結構的一部分。例如,maxTimeMS 選項可以與大多數指令一起傳遞,以限制特定指令在伺服器上執行的時間。

<?php

$manager
= new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$command = new MongoDB\Driver\Command(
array(
"distinct" => "beer",
"key" => "beer_name",
"maxTimeMS" => 10,
)
);

try {
$cursor = $manager->executeCommand("beerdb", $command);
$response = $cursor->toArray()[0];
} catch(
MongoDB\Driver\Exception\Exception $e) {
echo
$e->getMessage(), "\n";
exit;
}
var_dump($response);

?>

上述範例將輸出類似以下的內容


操作超過時間限制
新增註釋

使用者貢獻的註釋

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