php7中使用mongodb的驱动

6 篇文章 0 订阅
1 篇文章 0 订阅
一、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'=>'run', 'url' => 'http://www.run.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);
}

        5、MongoDB\Driver\Manager::executeReadCommand ( string $db , MongoDB\Driver\Command $command [, array$options = array() ] )执行读取数据的数据库命令

  和MongoDB\Driver\Manager::executeCommand用法一样

       6、MongoDB\Driver\Manager::executeReadWriteCommand( string $db , MongoDB\Driver\Command $command [, array$options = array() ] )执行读取写入文件的数据库命令

  和MongoDB\Driver\Manager::executeCommand用法一样

        7、MongoDB\Driver\Manager::executeWriteCommand ( string $db , MongoDB\Driver\Command $command [, array$options = array() ] )执行写入文件的数据库命令

  和MongoDB\Driver\Manager::executeCommand用法一样

        8、MongoDB\Driver\Manager::getReadConcern ( void )   //Return the ReadConcern for the Manager

$cursor = $conn->getReadPreference(); 
var_dump($cursor);
//object(MongoDB\Driver\ReadConcern)#2 (0) { }
         9、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" }

        10、MongoDB\Driver\Manager::getServers ( void )//返回该管理器连接的服务器

$conn->getServers();

        11、MongoDB\Driver\Manager::getWriteConcern ( void )//Return the WriteConcern for the Manager

$conn->getWriteConcern();

        12、MongoDB\Driver\Manager::selectServer( MongoDB\Driver\ReadPreference $readPreference )//选择匹配读取首选项的服务器

        13、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' => 'run', // 集合名称
        '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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值