【问题标题】:MongoClient Class vs. MongoDB\Driver\Manager ClassMongoClient 类与 MongoDB\Driver\Manager 类
【发布时间】:2017-08-16 05:36:55
【问题描述】:

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

不推荐使用定义此类的扩展。相反,应该使用 MongoDB 扩展。此类的替代品包括: MongoDB\驱动程序\管理器

我已经为 CRUD 使用了 MongoClient 类,但是在阅读了那句话之后,我尝试将 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


    【解决方案1】:

    以下是关于已弃用语言功能的一个很好的答案: What does PHP do with deprecated functions?

    这里是 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操作的教程有很多,例如:MongoDB PHP tutorial

    简而言之:出于安全原因,您不应该使用已弃用的功能,因为将来它可能会从 php 中删除。所以最好更新你的代码。

    【讨论】:

    • 谢谢!我可以问更多吗?什么是 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]);
    【解决方案2】:

    我个人发现没有指向 'vendor/autoload.php' 的链接。它在我的代码如下所示后开始工作:

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

    然后,如果您使用 MongoDB\Driver\Manager,这是一个现代版本的 MongoDB 驱动程序,您可以实现 CRUD 操作,如下所示:

    在集合中创建一个文档

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

    读取集合中的文档按名称和限制:

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

    读取集合中的文档由 MongoDb _id 限制:

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

    更新集合中的文档:(阅读更多关于选项 upsert 和 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);    
    

    删除集合中的文档 - 删除

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2018-07-24
      • 2022-01-25
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多