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))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题