Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda