dongnei3634 2017-06-26 08:49
浏览 124
已采纳

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条回答 默认 最新

  • doufu1939 2017-06-26 09:09
    关注
        $client = new \MongoDB\Client("mongodb://host:27017");
        var_dump(iterator_to_array($client->database_name->command(array('dbStats' => 1))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效