php7中使用mongodb的驱动

一、MongoDBDriverManager

  1、MongoDBDriverManager ([ string $uri = "mongodb://127.0.0.1/ [, array $uriOptions = array() [, array$driverOptions = array() ]]] )构造方法,连接mongodb数据库 

    $conn = new MongoDBDriverManager([ string $uri = "mongodb://127.0.0.1:27017/ [, array $uriOptions = array() [, array$driverOptions = array() ]]] );

    参数说明:

    uri:  mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

    uriOptions: 见http://php.net/manual/zh/mongodb-driver-manager.construct.php  
   driverOptions:http://php.net/manual/zh/mongodb-driver-manager.construct.php
 

2、MongoDBDriverManager::executeBulkWrite( string $namespace , MongoDBDriverBulkWrite $bulk [, array $options = array() ] ) 执行bulk的一些操作,包括混合操作
   $bulk = new MongoDBDriverBulkWrite();
   $bulk->delete([]);
   $bulk->insert(['x' => 7, 'name'=>'菜鸟教程', 'url' => 'http://www.runoob.com']);
$bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']);
$bulk->update(['x'=>2],['$set'=>['name'=>'谷歌']]);
$bulk->insert(['x' => 6, 'title'=>'淘宝', 'url' => 'http://www.taobao.com']);
   $conn->executeBulkWrite('test.runboo',$bulk);
   说明:test.runboo代表:数据库.集合名
3、MongoDBDriverManager::executeCommand( string $db , MongoDBDriverCommand $command [, array $options = array() ] ) 执行数据库命令
  
$query = ['name' => 'taobao']; // 查询条件
   $cmd = new MongoDBDriverCommand([
     'distinct' => 'runboo', // 集合名称
       'key' => 'url', // 需要显示的字段
      'query' => $query // 条件
   ]);
    $cursor = $conn->executeCommand('test', $cmd); // 数据库名称和命令
     $scents = $cursor->toArray(); // 转换成数组
   var_dump($scents); 4、MongoDBDriverManager::executeQuery  ( string $namespace , MongoDBDriverQuery $query [, array $options= array() ] )
  $filter = ['x' => ['$gt' => 0]];//查询条件
  $options = [
     'projection' => ['_id' => 0],
  ];
  $query = new MongoDBDriverQuery($filter,$options);//查询请求
  $rows = $conn->executeQuery('test.sites', $query); // 执行查询
  foreach($rows as $r){
     var_dump($r);
  }
4、MongoDBDriverManager::executeReadCommand ( string $db , MongoDBDriverCommand $command [, array$options = array() ] )执行读取数据的数据库命令
  和MongoDBDriverManager::executeCommand用法一样

5、MongoDBDriverManager::executeReadWriteCommand( string $db , MongoDBDriverCommand $command [, array$options = array() ] )执行读取写入文件的数据库命令
  和MongoDBDriverManager::executeCommand用法一样
6、MongoDBDriverManager::executeWriteCommand ( string $db , MongoDBDriverCommand $command [, array$options = array() ] )执行写入文件的数据库命令
  和MongoDBDriverManager::executeCommand用法一样
7、MongoDBDriverManager::getReadConcern ( void ) //Return the ReadConcern for the Manager
   
$cursor = $conn->getReadPreference();
    var_dump($cursor);

//object(MongoDBDriverReadConcern)#2 (0) { }
8、MongoDBDriverManager::getReadPreference ( void )   //Return the ReadPreference for the Manager
  $cursor = $conn->getReadPreference();
  var_dump($cursor);
  //object(MongoDBDriverReadPreference)#2 (1) { ["mode"]=> string(7) "primary" }
9、MongoDBDriverManager::getServers ( void )//返回该管理器连接的服务器
  $conn->getServers();
10、MongoDBDriverManager::getWriteConcern ( void )//Return the WriteConcern for the Manager
  $conn->getWriteConcern();
11、MongoDBDriverManager::selectServer( MongoDBDriverReadPreference $readPreference )//选择匹配读取首选项的服务器

12、MongoDBDriverManager::startSession([ array $options ] )//启动一个新客户端会话,以便与此客户端一起使用

二、MongoDBDriverBulkWrite 

  1、MongoDBDriverBulkWrite 混合写入操作(即插入、更新和删除)将是 组装成类型化写入命令,以便依次发送到服务器

    $bulk = new MongoDBDriverBulkWrite(['ordered' => true]);

    $bulk->insert(['_id' => 1, 'x' => 1]);

    $bulk->insert(['_id' => 2, 'x' => 2]);

    $bulk->update(['x' => 2], ['$set' => ['x' => 1]]);

    $bulk->insert(['_id' => 3, 'x' => 3]);

    $bulk->delete(['x' => 1]);

    $conn->executeBulkWrite('test.runboo',$bulk);

    说明:ordered有true和false,默认是false

   2、MongoDBDriverBulkWrite::count()  //计数-计算批量写入操作数    

    $bulk = new MongoDBDriverBulkWrite(['ordered' => true]);

    $bulk->insert(['_id' => 1, 'x' => 1]);

    $bulk->insert(['_id' => 2, 'x' => 2]);

    $bulk->update(['x' => 2], ['$set' => ['x' => 1]]);

    $bulk->insert(['_id' => 3, 'x' => 3]);

    $bulk->delete(['x' => 1]);

    $num = $bulk->count();

  3、MongoDBDriverBulkWrite::insert(array|object $document)

  4、MongoDBDriverBulkWrite::update(array|object $filter , array|object $newObj [, array $updateOptions ] )

  5、MongoDBDriverBulkWrite::delete(array|object $filter [, array $deleteOptions ])

三、MongoDBDriverCommand

  1、MongoDBDriverCommand创建一个新命令,结果是一个对象

    $query = ['name' => 'taobao']; // 查询条件

    $cmd = new MongoDBDriverCommand([

        'distinct' => 'runboo', // 集合名称

        'key' => 'url', // 需要显示的字段

        'query' => $query // 条件

    ]);

   $cursor = $conn->executeCommand('test', $cmd); // 数据库名称和命令

  $scents = $cursor->toArray(); // 转换成数组

  var_dump($scents);

四、MongoDBDriverQuery

  1、MongoDBDriverQuery($filter$options)创建一个新查询,结果是一个对象

  $filter = ['x' => ['$gt' => 0]];//查询条件
  $options = [
     'projection' => ['_id' => 0],
  ];
  $query = new MongoDBDriverQuery($filter,$options);//查询请求
  $rows = $conn->executeQuery('test.sites', $query); // 执行查询
  foreach($rows as $r){
     var_dump($r);
  }

五、MongoDBDriverWriteConcern  

  1、MongoDBDriverWriteConcern(string|integer $w [, int $wtimeout [, bool $journal ]])创造一个新的WriteConcern

    $w 是0,1,或者大于1的整数,MongoDBDriverWriteConcern::MAJORITY(常量)

  2、MongoDBDriverWriteConcern::bsonSerialize ( void )//返回用于序列化的对象

  3、MongoDBDriverWriteConcern::getJournal ( void )

  4、MongoDBDriverWriteConcern::getW ( void )

  5、MongoDBDriverWriteConcern::getWtimeout( void )

  6、MongoDBDriverWriteConcern::isDefault ( void )//检查是否为默认写入关系

六、MongoDBDriverReadPreference 

  1、MongoDBDriverReadPreference(string|integer $mode [, array $tagSets = NULL [, array$options = array() ]] )创造一个新的ReadPrefenerce

    $mode :

    MongoDBDriverReadPreference::RP_PRIMARY or "primary" 
    MongoDBDriverReadPreference::RP_PRIMARY_PREFERRED or "primaryPreferred" 
    MongoDBDriverReadPreference::RP_SECONDARY or "secondary" 
    MongoDBDriverReadPreference::RP_SECONDARY_PREFERRED or "secondaryPreferred" 
    MongoDBDriverReadPreference::RP_NEAREST or "nearest"

  2、MongoDBDriverReadPreference::bsonSerialize ( void )//返回用于序列化的对象

  3、MongoDBDriverReadPreference::getMaxStalenessSeconds( void )

  4、MongoDBDriverReadPreference::getMode( void )

  5、MongoDBDriverReadPreference::getTagSets( void )

七、MongoDBDriverReadConcern  

  1、MongoDBDriverReadConcern([ string $level ] )创造一个新的ReadConcern

    $level  https://docs.mongodb.com/manual/reference/read-concern/#read-concern-levels

  2、MongoDBDriverReadConcern  ::bsonSerialize ( void )//返回用于序列化的对象

  3、MongoDBDriverReadConcern  ::getLevel( void )

  4、MongoDBDriverReadConcern  ::isDefault ( void )//检查是否为默认读取关系

八、MongoDBDriverCursor 
  1、MongoDBDriverCursor()创建一个新光标(未使用)
  2、MongoDBDriverCursor::getId( void )//返回此游标的id
  3、MongoDBDriverCursor::getServer( void )//返回与此游标关联的服务器
  4、MongoDBDriverCursor::isDead( void )//检查游标是否可能具有额外结果
    5、MongoDBDriverCursor::setTypeMap ( array $typemap )//设置用于bson的类型映射
  6、MongoDBDriverCursor::toArray( void )

更多参见:http://php.net/manual/zh/book.mongodb.php     http://php.net/manual/zh/book.bson.php    http://php.net/manual/zh/mongodb.monitoring.php   http://php.net/manual/zh/mongodb.exceptions.php

原文地址:https://www.cnblogs.com/shelly520/p/8580938.html