Php MongoClient类与MongoDB\Driver\Manager类

Php MongoClient类与MongoDB\Driver\Manager类,php,mongodb,Php,Mongodb,我想得到你对我的网络项目的推荐。我使用PHP和MongoDB,但当我从PHP文档中读到这句话时,我感到困惑 此定义此类的扩展已弃用。相反,应该使用MongoDB扩展。这一类的替代方案包括: MongoDB\Driver\Manager 我已经将MongoClient类用于CRUD,但在阅读了这句话之后,我尝试将MongoClient迁移到MongoDB\Driver\Manager。使用MongoDB\Driver\Manager的连接已成功,但我无法继续:( 我的PHP版本是5.6.29。 M

我想得到你对我的网络项目的推荐。我使用PHP和MongoDB,但当我从PHP文档中读到这句话时,我感到困惑

此定义此类的扩展已弃用。相反,应该使用MongoDB扩展。这一类的替代方案包括: MongoDB\Driver\Manager

我已经将MongoClient类用于CRUD,但在阅读了这句话之后,我尝试将MongoClient迁移到MongoDB\Driver\Manager。使用MongoDB\Driver\Manager的连接已成功,但我无法继续:(

我的PHP版本是5.6.29。 Mongo扩展版本是1.7.0 MongoDB扩展版本是1.2.9

我的问题是: 我必须使用MongoDB\Driver\Manager类吗?
它比MongoClient类好吗?

下面是一个关于不推荐的语言功能的好答案:

以下是php与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) {
//...
}
关于php和mongodb的CRUD操作,有很多教程,例如:


简言之:由于安全原因以及将来可能从php中删除的原因,您不应该使用不推荐的功能。因此,最好更新您的代码。

我个人遇到的问题是缺少指向'vendor/autoload.php'的链接。在我的代码看起来如下所示后,该功能开始工作:

  $DB_CONNECTION_STRING="mongodb://YourCredentials";
  require '../vendor/autoload.php';
  $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );
然后,如果使用现代版MongoDB驱动程序MongoDB\Driver\Manager,则可以实现如下CRUD操作:

  $DB_CONNECTION_STRING="mongodb://YourCredentials";
  require '../vendor/autoload.php';
  $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );
在集合中创建文档:

按名称读取集合中的文档,但有限制:

$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);    
通过MongoDb\u id读取集合中的文档,但有限制:

$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);    
更新集合中的文档:(阅读有关选项upsert和multi的更多信息)

删除集合中的文档-删除:


谢谢!我可以问更多吗?什么是MongoDB\Driver\Command类?我不明白它是什么时候使用的。“MongoDB\Driver\Command类是一个表示数据库命令的值对象。”例如:
$Command=new MongoDB\Driver\Command(['ping'=>1]);
$Command=new MongoDB\Driver\Command([“count”=>“collection”,“query”=>$query];
使用以下命令,您可以对包含此字符串“world”的文档进行计数:
$query=[“hello”=>“world”];$command=new MongoDB\Driver\command([“count”=>“collection”,“query”=>$query]);