0

image for mongo info

MongoDB shell version v3.4.1

mongodb driver v1.6.14

xampp v3.2.1, Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12

i m running for first time and getting this problem plz help me.

Fatal error: Class 'MongoDB\Driver\Manager' not found in H:\xampp\htdocs\www\phpmongodb\vendor\mongodb\mongodb\src\Client.php on line 81

iram ch
  • 11
  • 2

1 Answers1

0

I had the same error before I set up the mongodb added the following link to 'vendor/autoload.php'. In many cases you need to check your hosting. It started working after my code looked like the following:

  $DB_CONNECTION_STRING="mongodb://YourCredentials";
  require '../../vendor/autoload.php';
  $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );

Then if you use MongoDB\Driver\Manager, a modern version of MongoDB driver, you have something looks such as this:

Create a document in the collection:

$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc = ['name' => 'John', age => 33, profession => 'Guess what?'];
$bulkWrite->insert($doc);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);

Read document in the collection by name with a limit:

$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);

Read document in the collection by MongoDb _id with a limit:

$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);    

Update document in the collection: (Read more about options upsert and multi here)

$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
$options = ['multi' => false, 'upsert' => false];
$bulkWrite->update($filter, $update, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);    

Delete document in the collection - Delete:

$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John', age => 35];
$options = ['limit' => 1];
$bulkWrite->delete($filter, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);
Roman
  • 19,236
  • 15
  • 93
  • 97