文档菜单
文档首页
/ / /
PHP库手册
/ /

MongoDB\Collection::explain()

本页内容

  • 定义
  • 参数
  • 返回值
  • 错误/异常
  • 可解释命令
  • 示例
  • 另请参阅

新增版本1.4.

MongoDB\Collection::explain()

解释给定命令。

function explain(
MongoDB\Operation\Explainable $explainable,
array $options = []
): array|object
$explainable : MongoDB\Operation\Explainable
要解释的命令。
$options : 数组

指定所需选项的数组。

名称
类型
描述
评论
混合

允许用户指定任意评论以帮助通过数据库分析器当前操作输出和日志。

此选项自 MongoDB 4.4 版本起可用,如果指定用于旧版本服务器,则会在执行时引发异常。

默认为所解释操作(如果有的话)的 评论

新增版本1.13.

readPreference

用于操作的读取偏好。默认为集合的读取偏好。

typeMap
数组

要应用于游标的类型映射,它确定 BSON 文档如何转换为 PHP 值。默认为集合的类型映射。

这将用于返回的命令结果文档。

verbosity
字符串
运行命令的详细程度。有关更多信息,请参阅explain命令。

一个包含 explain 命令结果的数组或对象。返回类型将取决于 typeMap 选项。

MongoDB\Exception\UnsupportedException 如果使用了选定的服务器不支持(例如 collationreadConcernwriteConcern)的选项。

MongoDB\Exception\InvalidArgumentException 与参数或选项解析相关的错误。

MongoDB\Driver\Exception\RuntimeException)对于扩展级别(例如连接错误)的其他错误。

解释性命令包括但不限于

  • MongoDB\Operation\Aggregate

  • MongoDB\Operation\Count

  • MongoDB\Operation\DeleteMany

  • MongoDB\Operation\DeleteOne

  • MongoDB\Operation\Distinct

  • MongoDB\Operation\Find

  • MongoDB\Operation\FindOne

  • MongoDB\Operation\FindOneAndDelete

  • MongoDB\Operation\FindOneAndReplace

  • MongoDB\Operation\FindOneAndUpdate

  • MongoDB\Operation\UpdateMany

  • MongoDB\Operation\UpdateOne

本例解释了count命令。

<?php
$collection = (new MongoDB\Client)->test->restaurants;
$count = new MongoDB\Operation\Count(
$collection->getDatabaseName(),
$collection->getCollectionName(),
['cuisine' => 'Italian']
);
$result = $collection->explain($count);
var_dump($result);

输出将类似于

object(MongoDB\Model\BSONDocument)#29 (1) {
["storage":"ArrayObject":private]=>
array(4) {
["queryPlanner"]=>
object(MongoDB\Model\BSONDocument)#21 (1) {
["storage":"ArrayObject":private]=>
array(6) {
["plannerVersion"]=>
int(1)
["namespace"]=>
string(16) "test.restaurants"
["indexFilterSet"]=>
bool(false)
["parsedQuery"]=>
object(MongoDB\Model\BSONDocument)#15 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["cuisine"]=>
object(MongoDB\Model\BSONDocument)#14 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["$eq"]=>
string(7) "Italian"
}
}
}
}
["winningPlan"]=>
object(MongoDB\Model\BSONDocument)#19 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["stage"]=>
string(5) "COUNT"
["inputStage"]=>
object(MongoDB\Model\BSONDocument)#18 (1) {
["storage":"ArrayObject":private]=>
array(3) {
["stage"]=>
string(8) "COLLSCAN"
["filter"]=>
object(MongoDB\Model\BSONDocument)#17 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["cuisine"]=>
object(MongoDB\Model\BSONDocument)#16 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["$eq"]=>
string(7) "Italian"
}
}
}
}
["direction"]=>
string(7) "forward"
}
}
}
}
["rejectedPlans"]=>
object(MongoDB\Model\BSONArray)#20 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
}
}
["executionStats"]=>
object(MongoDB\Model\BSONDocument)#27 (1) {
["storage":"ArrayObject":private]=>
array(7) {
["executionSuccess"]=>
bool(true)
["nReturned"]=>
int(0)
["executionTimeMillis"]=>
int(24)
["totalKeysExamined"]=>
int(0)
["totalDocsExamined"]=>
int(25359)
["executionStages"]=>
object(MongoDB\Model\BSONDocument)#25 (1) {
["storage":"ArrayObject":private]=>
array(14) {
["stage"]=>
string(5) "COUNT"
["nReturned"]=>
int(0)
["executionTimeMillisEstimate"]=>
int(20)
["works"]=>
int(25361)
["advanced"]=>
int(0)
["needTime"]=>
int(25360)
["needYield"]=>
int(0)
["saveState"]=>
int(198)
["restoreState"]=>
int(198)
["isEOF"]=>
int(1)
["invalidates"]=>
int(0)
["nCounted"]=>
int(1069)
["nSkipped"]=>
int(0)
["inputStage"]=>
object(MongoDB\Model\BSONDocument)#24 (1) {
["storage":"ArrayObject":private]=>
array(14) {
["stage"]=>
string(8) "COLLSCAN"
["filter"]=>
object(MongoDB\Model\BSONDocument)#23 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["cuisine"]=>
object(MongoDB\Model\BSONDocument)#22 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["$eq"]=>
string(7) "Italian"
}
}
}
}
["nReturned"]=>
int(1069)
["executionTimeMillisEstimate"]=>
int(20)
["works"]=>
int(25361)
["advanced"]=>
int(1069)
["needTime"]=>
int(24291)
["needYield"]=>
int(0)
["saveState"]=>
int(198)
["restoreState"]=>
int(198)
["isEOF"]=>
int(1)
["invalidates"]=>
int(0)
["direction"]=>
string(7) "forward"
["docsExamined"]=>
int(25359)
}
}
}
}
["allPlansExecution"]=>
object(MongoDB\Model\BSONArray)#26 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
}
}
["serverInfo"]=>
object(MongoDB\Model\BSONDocument)#28 (1) {
["storage":"ArrayObject":private]=>
array(4) {
["host"]=>
string(9) "localhost"
["port"]=>
int(27017)
["version"]=>
string(5) "3.6.1"
["gitVersion"]=>
string(40) "025d4f4fe61efd1fb6f0005be20cb45a004093d1"
}
}
["ok"]=>
float(1)
}
}
  • explain 命令参考 MongoDB 手册

返回

estimatedDocumentCount()