PHP MongoDB获取统计信息

I'm currently trying to retrieve the statistics of a mongodb by php. I already found the way to run the command (https://stackoverflow.com/a/11650724/2889265), but than executing it, the response is completly different to the expected result of executing db.stats() directly on the database. Here's what I got:

Result PHP $this->database->command(array('dbStats' => 1));:

MongoDB\Driver\Cursor Object
(
    [database] => dev
    [collection] => 
    [query] => 
    [command] => MongoDB\Driver\Command Object
        (
            [command] => stdClass Object
                (
                    [dbStats] => 1
                )

        )

    [readPreference] => MongoDB\Driver\ReadPreference Object
        (
            [mode] => primary
        )

    [isDead] => 
    [currentIndex] => 0
    [currentDocument] => 
    [server] => MongoDB\Driver\Server Object
        (
            [host] => xxx.xxx.xxx.xxx
            [port] => 27017
            [type] => 1
            [is_primary] => 
            [is_secondary] => 
            [is_arbiter] => 
            [is_hidden] => 
            [is_passive] => 
            [last_is_master] => Array
                (
                    [ismaster] => 1
                    [maxBsonObjectSize] => 16777216
                    [maxMessageSizeBytes] => 48000000
                    [maxWriteBatchSize] => 1000
                    [localTime] => MongoDB\BSON\UTCDateTime Object
                        (
                            [milliseconds] => 1498465564742
                        )

                    [maxWireVersion] => 2
                    [minWireVersion] => 0
                    [ok] => 1
                )

            [round_trip_time] => 12
        )

)

Result db.stats()

{
        "db" : "dev",
        "collections" : 21,
        "objects" : 7153120,
        "avgObjSize" : 818.2133580871005,
        "dataSize" : 5852778336,
        "storageSize" : 6882250752,
        "numExtents" : 71,
        "indexes" : 26,
        "indexSize" : 866189968,
        "fileSize" : 8519680000,
        "nsSizeMB" : 16,
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 5
        },
        "extentFreeList" : {
                "num" : 0,
                "totalSize" : 0
        },
        "ok" : 1
}

Am I doing it wrong and what do I have to do, to get the statistics like db.stats()?

I'm using php 7.1 with the composer package mongodb/mongodb. The MongoDB has version 2.6.10.

The second problem, then I execute $this->database->command(array('collStats' => "dev")); i get a RuntimeException "Collection [dev.dev] not found.". Why? Found the reason for my RuntimeException... I requested "dev" as collectionname, but it's the name of the database.

1个回答



  $ client = new \ MongoDB \ Client(“mongodb:// host:27017”); 
var_dump(iterator_to_array ($ client-> database_name-> command(array('dbStats'=> 1))));
</ code> </ pre>
</ div>

展开原文

原文

    $client = new \MongoDB\Client("mongodb://host:27017");
    var_dump(iterator_to_array($client->database_name->command(array('dbStats' => 1))));

dsh1102
dsh1102 你有没有想法,为什么我得到RuntimeException?
大约 3 年之前 回复
duaiwu8385
duaiwu8385 感谢您的答复。 iterator_to_array()函数是一个小但有用的提示。 我发现了自己的错误。 我只查看了命令的结果,但没有进入MongoDB \ Driver \ Cursor对象。
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问