Jihyang Lee
Jihyang Lee

Reputation: 63

MongoClient Class vs. MongoDB\Driver\Manager Class

I want to get your recommend about my web project. I use PHP and MongoDB but I was confused when I read this sentence from php documentation.

This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include: MongoDB\Driver\Manager

I already used MongoClient Class for CRUD but after reading that sentence, I tried to migrate MongoClient to MongoDB\Driver\Manager. The connection using MongoDB\Driver\Manager was successed but I couldn't anymore :(

My PHP version is 5.6.29. Mongo extension version is 1.7.0 MongoDB extension version is 1.2.9

My questions are: Do I have to use MongoDB\Driver\Manager Class? Is it better than MongoClient Class?

Upvotes: 3

Views: 4125

Answers (2)

Roman
Roman

Reputation: 21815

I personally came across with the absence of a link to 'vendor/autoload.php'. 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 implement CRUD operations such as the following:

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

Upvotes: 1

Meteor Newbie
Meteor Newbie

Reputation: 686

Here is a good answer about deprecated language features: What does PHP do with deprecated functions?

And here is a proper usage for php with mongodb:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$filter = [];
$options = [
    'sort' => ['_id' => 1],
];

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('db.collection', $query);

foreach ($cursor as $document) {
//...
}

There are are a lot of tutorials for CRUD operation with php and mongodb, for example: MongoDB PHP tutorial

In short: you should not use deprecated feature because of security reasons and because it could get removed from php in the future. So better update your code.

Upvotes: 5

Related Questions