【问题标题】:MongoDB command in PHP (db.serverStatus().uptime)PHP 中的 MongoDB 命令 (db.serverStatus().uptime)
【发布时间】:2022-01-22 05:36:06
【问题描述】:

我正在尝试从远程服务器获取正常运行时间,到目前为止,我能够通过执行命令来获取版本。但我无法让它运行这个命令来获得正常运行时间。

<?php
    
    
    $mongo = new MongoClient('mongodb://<user>:<pass>@<DNS>:<PORT>');
    
    echo "Connection to database successfully\n";
    
    $admin = $mongo->admin;
    
    $cursor= $admin->command(array('serverStatus().uptime'));
    
    var_dump($cursor);
    
    $response = $cursor->toArray();
    
    var_dump($response);
    
?>

我正在运行一个 var_dump 来查看它返回的内容,它确实返回了一个 int,但它太大而不能成为实际的正常运行时间。

【问题讨论】:

  • 结果以毫秒为单位,您需要除以 60 或 3600 或 86400 才能获得以分钟、小时或天为单位的结果。

标签: php mongodb


【解决方案1】:

要获取服务器正常运行时间,请执行 serverStatus 命令,然后您可以像这样以毫秒为单位获取正常运行时间

try {
    $dsn = "mongodb://username:password@127.0.0.1:27017/";
    
    $manager = new MongoDB\Driver\Manager($dsn);
    $command = new MongoDB\Driver\Command(['serverStatus' => 1]);
    $cursor = $manager->executeCommand('admin', $command);

    foreach ($cursor->toArray() as $server) {
        //print_r($server->uptime / 60. " Minutes");
        //print_r($server->uptime / 3600. " Hours");
        print_r($server->uptime / 86400. " Days");
    }
} catch (\MongoDB\Driver\Exception\Exception $e) {  
    print_r($e);
}

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    相关资源
    最近更新 更多