php7中使用mongodb的驅動


 

一、MongoDB\Driver\Manager

  1、MongoDB\Driver\Manager ([ string $uri = "mongodb://127.0.0.1/ [, array $uriOptions = array() [, array$driverOptions = array() ]]] )構造方法,連接mongodb數據庫 

    $conn = new MongoDB\Driver\Manager([ 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、MongoDB\Driver\Manager::executeBulkWrite( string $namespace , MongoDB\Driver\BulkWrite $bulk [, array $options = array() ] ) 執行bulk的一些操作,包括混合操作
   $bulk = new MongoDB\Driver\BulkWrite();
   $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、MongoDB\Driver\Manager::executeCommand( string $db , MongoDB\Driver\Command $command [, array $options = array() ] ) 執行數據庫命令
  
$query = ['name' => 'taobao']; // 查詢條件
   $cmd = new MongoDB\Driver\Command([
     'distinct' => 'runboo', // 集合名稱
       'key' => 'url', // 需要顯示的字段
      'query' => $query // 條件
   ]);
    $cursor = $conn->executeCommand('test', $cmd); // 數據庫名稱和命令
     $scents = $cursor->toArray(); // 轉換成數組
   var_dump($scents); 4、MongoDB\Driver\Manager::executeQuery  ( string $namespace , MongoDB\Driver\Query $query [, array $options= array() ] )
  $filter = ['x' => ['$gt' => 0]];//查詢條件
  $options = [
     'projection' => ['_id' => 0],
  ];
  $query = new MongoDB\Driver\Query($filter,$options);//查詢請求
  $rows = $conn->executeQuery('test.sites', $query); // 執行查詢
  foreach($rows as $r){
     var_dump($r);
  }
4、MongoDB\Driver\Manager::executeReadCommand ( string $db , MongoDB\Driver\Command $command [, array$options = array() ] )執行讀取數據的數據庫命令
  和MongoDB\Driver\Manager::executeCommand用法一樣

5、MongoDB\Driver\Manager::executeReadWriteCommand( string $db , MongoDB\Driver\Command $command [, array$options = array() ] )執行讀取寫入文件的數據庫命令
  和MongoDB\Driver\Manager::executeCommand用法一樣
6、MongoDB\Driver\Manager::executeWriteCommand ( string $db , MongoDB\Driver\Command $command [, array$options = array() ] )執行寫入文件的數據庫命令
  和MongoDB\Driver\Manager::executeCommand用法一樣
7、MongoDB\Driver\Manager::getReadConcern ( void ) //Return the ReadConcern for the Manager
   
$cursor = $conn->getReadPreference();
    var_dump($cursor);

//object(MongoDB\Driver\ReadConcern)#2 (0) { }
8、MongoDB\Driver\Manager::getReadPreference ( void )   //Return the ReadPreference for the Manager
  $cursor = $conn->getReadPreference();
  var_dump($cursor);
  //object(MongoDB\Driver\ReadPreference)#2 (1) { ["mode"]=> string(7) "primary" }
9、MongoDB\Driver\Manager::getServers ( void )//返回該管理器連接的服務器
  $conn->getServers();
10、MongoDB\Driver\Manager::getWriteConcern ( void )//Return the WriteConcern for the Manager
  $conn->getWriteConcern();
11、MongoDB\Driver\Manager::selectServer( MongoDB\Driver\ReadPreference $readPreference )//選擇匹配讀取首選項的服務器

12、MongoDB\Driver\Manager::startSession([ array $options ] )//啟動一個新客戶端會話,以便與此客戶端一起使用

二、MongoDB\Driver\BulkWrite 

  1、MongoDB\Driver\BulkWrite 混合寫入操作(即插入、更新和刪除)將是 組裝成類型化寫入命令,以便依次發送到服務器

    $bulk = new MongoDB\Driver\BulkWrite(['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、MongoDB\Driver\BulkWrite::count()  //計數-計算批量寫入操作數    

    $bulk = new MongoDB\Driver\BulkWrite(['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、MongoDB\Driver\BulkWrite::insert(array|object $document)

  4、MongoDB\Driver\BulkWrite::update(array|object $filter , array|object $newObj [, array $updateOptions ] )

  5、MongoDB\Driver\BulkWrite::delete(array|object $filter [, array $deleteOptions ])

 

三、MongoDB\Driver\Command

  1、MongoDB\Driver\Command創建一個新命令,結果是一個對象

    $query = ['name' => 'taobao']; // 查詢條件

    $cmd = new MongoDB\Driver\Command([

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

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

        'query' => $query // 條件

    ]);

   $cursor = $conn->executeCommand('test', $cmd); // 數據庫名稱和命令

  $scents = $cursor->toArray(); // 轉換成數組

  var_dump($scents);

四、MongoDB\Driver\Query

  1、MongoDB\Driver\Query($filter$options)創建一個新查詢,結果是一個對象

  $filter = ['x' => ['$gt' => 0]];//查詢條件
  $options = [
     'projection' => ['_id' => 0],
  ];
  $query = new MongoDB\Driver\Query($filter,$options);//查詢請求
  $rows = $conn->executeQuery('test.sites', $query); // 執行查詢
  foreach($rows as $r){
     var_dump($r);
  }

 

五、MongoDB\Driver\WriteConcern  

 

  1、MongoDB\Driver\WriteConcern(string|integer $w [, int $wtimeout [, bool $journal ]])創造一個新的WriteConcern

    $w 是0,1,或者大於1的整數,MongoDB\Driver\WriteConcern::MAJORITY(常量)

  2、MongoDB\Driver\WriteConcern::bsonSerialize ( void )//返回用於序列化的對象

  3、MongoDB\Driver\WriteConcern::getJournal ( void )

  4、MongoDB\Driver\WriteConcern::getW ( void )

  5、MongoDB\Driver\WriteConcern::getWtimeout( void )

  6、MongoDB\Driver\WriteConcern::isDefault ( void )//檢查是否為默認寫入關系

六、MongoDB\Driver\ReadPreference 

  1、MongoDB\Driver\ReadPreference(string|integer $mode [, array $tagSets = NULL [, array$options = array() ]] )創造一個新的ReadPrefenerce

    $mode :

    MongoDB\Driver\ReadPreference::RP_PRIMARY or "primary" 
    MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED or "primaryPreferred" 
    MongoDB\Driver\ReadPreference::RP_SECONDARY or "secondary" 
    MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED or "secondaryPreferred" 
    MongoDB\Driver\ReadPreference::RP_NEAREST or "nearest"

  2、MongoDB\Driver\ReadPreference::bsonSerialize ( void )//返回用於序列化的對象

  3、MongoDB\Driver\ReadPreference::getMaxStalenessSeconds( void )

  4、MongoDB\Driver\ReadPreference::getMode( void )

  5、MongoDB\Driver\ReadPreference::getTagSets( void )

七、MongoDB\Driver\ReadConcern  

  1、MongoDB\Driver\ReadConcern([ string $level ] )創造一個新的ReadConcern

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

  2、MongoDB\Driver\ReadConcern  ::bsonSerialize ( void )//返回用於序列化的對象

  3、MongoDB\Driver\ReadConcern  ::getLevel( void )

  4、MongoDB\Driver\ReadConcern  ::isDefault ( void )//檢查是否為默認讀取關系

八、MongoDB\Driver\Cursor 
  1、MongoDB\Driver\Cursor()創建一個新光標(未使用)
  2、MongoDB\Driver\Cursor::getId( void )//返回此游標的id
  3、MongoDB\Driver\Cursor::getServer( void )//返回與此游標關聯的服務器
  4、MongoDB\Driver\Cursor::isDead( void )//檢查游標是否可能具有額外結果
    5、MongoDB\Driver\Cursor::setTypeMap ( array $typemap )//設置用於bson的類型映射
  6、MongoDB\Driver\Cursor::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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM