3

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?

Meteor Newbie
  • 646
  • 2
  • 10
  • 23
Jihyang Lee
  • 63
  • 1
  • 4

2 Answers2

5

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.

Meteor Newbie
  • 646
  • 2
  • 10
  • 23
  • Thanks! Can I ask more? What is MongoDB\Driver\Command Class? I can't understand when it is used. – Jihyang Lee Aug 18 '17 at 06:35
  • "The MongoDB\Driver\Command class is a value object that **represents a database command**." For Example: `$command = new MongoDB\Driver\Command(['ping' => 1]);` or `$command = new MongoDB\Driver\Command(["count" => "collection", "query" => $query]);` – Meteor Newbie Aug 18 '17 at 08:21
  • With following command you can count documents that contain this string "world": `$query = ["hello" => "world"]; $command = new MongoDB\Driver\Command(["count" => "collection", "query" => $query]);` – Meteor Newbie Aug 18 '17 at 08:30
1

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);
Roman
  • 19,236
  • 15
  • 93
  • 97