(mongodb >=1.0.0)
MongoDB\Driver\Command::__construct — 创建一个新的命令
构造一个新的 MongoDB\Driver\Command,它是一个不可变的值对象,表示数据库命令。然后可以使用 MongoDB\Driver\Manager::executeCommand() 执行该命令。
完整的命令文档(包括命令名称及其选项)应在 document 参数中表达。 commandOptions 参数仅用于指定与命令执行和生成的 MongoDB\Driver\Cursor 相关的选项。
| 版本 | 描述 |
|---|---|
| PECL mongodb 1.4.0 |
添加了第二个 |
示例 #1 MongoDB\Driver\Command::__construct() 示例
<?php
$manager = new MongoDB\Driver\Manager("mongodb://: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://: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);
?>上面的示例将输出类似以下内容