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

293
Vistas
MongoDB\Driver\Manager Queries

I'm using mongodb driver to connect to mongodb through PHP. I was unable to connect to mongodb using mongoClient().

Versions of my php and mongodb

php --version output 1 mongo --version output 2

so, I tried to connect it through mongodb driver. Here's the code...

$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(["number"=>$Number, "created"=>time()]);
$connection-> executeBulkWrite("dbName.collectionName", $bulk);

This is working fine to insert the data to mongodb.

Now, I want to get the no. of rows from a collection in database but unable to find any solution.

I tried this code for retrieving data from mongodb:

try {
    $connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
    $filter = ["number"=> $Number];
    $query_mongo = new MongoDB\Driver\Query($filter);
    $mongoResult = $connection->executeQuery("dbName.collectionName", $query_mongo);
    $result = current($mongoResult->toArray());
    var_dump($result);
    } catch (MongoDB\Driver\Exception\Exception $e) {
      $filename = basename(__FILE__);
      echo "The $filename script has experienced an error.\n"; 
      echo "It failed with the following exception:\n";
      echo "Exception:", $e->getMessage(), "\n";
      echo "In file:", $e->getFile(), "\n";
      echo "On line:", $e->getLine(), "\n";
    }

But I'm getting this error in browser console...

parsererror,SyntaxError: Unexpected token o in JSON at position 0,object(stdClass)#26 (3) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#25 (1) {
    ["oid"]=>
    string(24) "587c6e37e2d11616411c90a2"
  }
  ["number"]=>
  string(10) "123456"
  ["created"]=>
  int(1484549687)
}

Q1. can I get rid of this Mongodb\Driver\Manager and use the old mongoClient and How?

Q2. I want to get no. of rows in the table. How can i do that using this Driver\Manager?

Q3. Any solution to the given error?

Thanks in advance...

over 4 years ago · Santiago Trujillo
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