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

289
Views
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
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!