Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
MongoDB command in PHP (db.serverStatus().uptime)

Im trying to get the uptime from a remote server, so far im able to get the version through the execution of a command. But I can't get it to run this command to get the uptime.

<?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);
    
?>

Im running a var_dump to see what it returns, it does return a int but is far too big to be the actual uptime.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To get the server uptime, execute serverStatus command and then you can fetch uptime in milliseconds like this

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);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!