php slaveok_PHP操作MongoDB实例 — ttlsa教程系列之mongodb(十一)

# https://github.com/mongodb/mongo-php-driver/archive/master.zip

# unzip master.zip

# /usr/local/php/bin/phpize

# ./config --with-php-config=/usr/local/php/bin/php-config

# make

# make install

1

2

3

4

5

6

# https://github.com/mongodb/mongo-php-driver/archive/master.zip

# unzip master.zip

# /usr/local/php/bin/phpize

# ./config --with-php-config=/usr/local/php/bin/php-config

# make

# make install

将mongo.so添加到php.ini

# vim /usr/local/php/etc/php.ini

extension = mongo.so

1

2

# vim /usr/local/php/etc/php.ini

extension=mongo.so

重启php-fpm生效

# /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.comf

1

# /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.comf

php的mongodb扩展,提供了4个核心类接口

1). 针对mongodb的连接操作类MongoClient

http://www.php.net/manual/zh/class.mongoclient.php

2). 针对mongodb的数据库操作类MongoDB

http://www.php.net/manual/zh/class.mongodb.php

3). 针对mongodb的集合操作类MongoCollection

http://www.php.net/manual/zh/class.mongocollection.php

4). 针对mongodb的查询结果集(游标)操作类MongoCursor

http://www.php.net/manual/zh/class.mongocursor.php

连接MongoDB

mongodb驱动连接格式为:mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[database][?options]],如:

mongodb://localhost

mongodb://user:password@localhost

mongodb://user:password@localhost/database

mongodb://example1.com:27017,example2.com:27017

mongodb://localhost,localhost:27018,localhost:27019

mongodb://host1,host2,host3/?slaveOk=true

mongodb://localhost/?safe=true

mongodb://host1,host2,host3/?safe=true;w=2;wtimeoutMS=2000

mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName

mongodb://localhost/?journal=true&w=majority&wTimeoutMS=20000

具体含义参见《ttlsa教程系列之mongodb—(一)mongodb介绍》 http://www.ttlsa.com/html/1594.html

PHP连接实例:

$m = new MongoClient("mongodb://localhost/?journal=true&w=majority&wTimeoutMS=20000");

$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName"));

$m = new MongoClient("mongodb://rs1.example.com:27017", array("replicaSet" => "myReplSetName"));

$m = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017", array("replicaSet" => "myReplSetName","wTimeoutMS"=>20000));

1

2

3

4

5

$m=newMongoClient("mongodb://localhost/?journal=true&w=majority&wTimeoutMS=20000");

$m=newMongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName"));

$m=newMongoClient("mongodb://rs1.example.com:27017",array("replicaSet"=>"myReplSetName"));

$m=newMongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017",array("replicaSet"=>"myReplSetName","wTimeoutMS"=>20000));

在连接到复制集时,用它来判断哪台是primary。返回主机名称、端口号、健康程度、状态(1-primary,2-secondary,0-other)、ping耗时、前一次ping的时间戳。

$m->getHosts();

1

$m->getHosts();

列出所有数据库,返回数据库名称、大小、是否为空以及总大小、ok状态。

$m->listDBs();

1

$m->listDBs();

选择数据库,返回数据库对象

$db = $m->db_name;

$db = $m->selectDB(db_name);

1

2

3

$db=$m->db_name;

$db=$m->selectDB(db_name);

选择表(集合),返回文档集合对象

$col = $m->selectCollection(db_name, col_name);

$col = $m->selectDB(db_name)->selectCollection(col_name);

$col = $m->db_name->col_name;

1

2

3

4

5

$col=$m->selectCollection(db_name,col_name);

$col=$m->selectDB(db_name)->selectCollection(col_name);

$col=$m->db_name->col_name;

列出所有集合,返回集合对象

$col_list = $db->listCollections();

1

$col_list=$db->listCollections();

获取当前选择的数据库名,返回数据库名

$db_name = $db->__toString();

1

$db_name=$db->__toString();

删除当前数据库

$db->drop();

1

$db->drop();

设置slaveok状态(可读状态设置)

$db->setSlaveOkay(true/false);

1

$db->setSlaveOkay(true/false);

获取slaveok当前状态

$db->getSlaveOkay();

1

$db->getSlaveOkay();

插入数据MongoCollection::insert(array $a,array $options)

array $a 要插入的数组

array $options 选项:safe 是否返回操作结果信息;fsync 是否直接插入到物理硬盘;w 写入份数;timeout 超时时间

$coll = $m->db_name->col_name;

$a = array(’website'=>’www.ttlsa.com');

$options = array(’safe’=>true);

$rs = $coll->insert($a,$options); $rs为数组,包含操作信息

1

2

3

4

5

$coll=$m->db_name->col_name;

$a=array(’website'=>’www.ttlsa.com');

$options=array(’safe’=>true);

$rs=$coll->insert($a,$options);$rs为数组,包含操作信息

删除集合中的记录MongoCollection::remove(array $criteria,array $options)

array $criteria 条件

array $options 选项: safe 是否返回操作结果; fsync 是否是直接影响到物理硬盘; justOne 是否只影响一条记录

$coll = $m->db_name->col_name;

$c = array(’website'=>'www.ttlsa.com',’hit’=>array(’$lt’=>100));

$options = array(’safe’=>true);

$rs = $coll->remove($c,$options); $rs为数组,包含操作信息

1

2

3

4

5

$coll=$m->db_name->col_name;

$c=array(’website'=>'www.ttlsa.com',’hit’=>array(’$lt’=>100));

$options=array(’safe’=>true);

$rs=$coll->remove($c,$options);$rs为数组,包含操作信息

更新集合中的记录MongoCollection::update(array $criceria,array $newobj,array $options)

array $criteria 条件

array $newobj 要更新的内容

array $options 选项: safe 是否返回操作结果; fsync 是否是直接影响到物理硬盘; upsert 是否没有匹配数据就添加一条新的; multiple 是否影响所有符合条件的记录,默认只影响一条

$coll = $m->db_name->coll_name;

$c = array(’uid'=>888,’login_count’=>array(’$lt’=>100));

$newobj = array(’vip'=>’1',’score'=>’10000');

$options = array(’safe’=>true,’multiple’=>true);

$rs = $coll->remove($c,$newobj,$options); $rs为数组,包含操作信息

1

2

3

4

5

6

$coll=$m->db_name->coll_name;

$c=array(’uid'=>888,’login_count’=>array(’$lt’=>100));

$newobj = array(’vip'=>’1',’score'=>’10000');

$options=array(’safe’=>true,’multiple’=>true);

$rs=$coll->remove($c,$newobj,$options);$rs为数组,包含操作信息

查询集合获取单条记录MongoCollection::findOne(array $query,array $fields)

array $query 条件

array $fields 要获得的字段

$coll = $m->db_name->col_name;

$query = array(’score’=>array(’$lt’=>10000));

$fields = array(’uid'=>true,’vip'=>true);

$rs = $coll->findOne($query,$fields); 返回array或null

1

2

3

4

5

$coll=$m->db_name->col_name;

$query=array(’score’=>array(’$lt’=>10000));

$fields=array(’uid'=>true,’vip'=>true);

$rs=$coll->findOne($query,$fields);返回array或null

获取多条记录MongoCollection::find(array $query,array $fields)

array $query 条件

array $fields 要获得的字段

$coll = $m->db_name->col_name;

$query = array(’s’=>array(’$lt’=>100));

$query = array(’score’=>array(’$lt’=>10000));

$fields = array(’uid'=>true,’vip'=>true);

$rs = $coll->find($query,$fields); 返回游标对象MongoCursor。

1

2

3

4

5

6

$coll=$m->db_name->col_name;

$query=array(’s’=>array(’$lt’=>100));

$query=array(’score’=>array(’$lt’=>10000));

$fields=array(’uid'=>true,’vip'=>true);

$rs=$coll->find($query,$fields);返回游标对象MongoCursor。

获取查询结果数量

$cursor = $coll->find();

$num = $cursor->count();

1

2

$cursor=$coll->find();

$num=$cursor->count();

选定列MongoCursor::fields

$cursor->fields(array(column_name1 => true, column_name2 => false));

$cursor = $coll->find()->fields(array(column_name1 => true, column_name2 => false));

1

2

3

$cursor->fields(array(column_name1=>true,column_name2=>false));

$cursor=$coll->find()->fields(array(column_name1=>true,column_name2=>false));

分页

$cursor = $coll->find()->limit(30)->skip(0);

1

$cursor=$coll->find()->limit(30)->skip(0);

排序MongoCursor::sort

$cursor = $coll->find()->sort(array(column_name1 => -1, column_name2 => 1));

1

$cursor=$coll->find()->sort(array(column_name1=>-1,column_name2=>1));

取查询结果

$cursor = $coll->find($query,$fields);

while($cursor->hasNext()){

$r = $cursor->getNext();

var_dump($r);

}

或者

$cursor = $coll->find($query,$fields);

$r = array();

foreache($cursor as $k=>$v){

var_dump($v);

$r[] = $v;

}

或者

$cursor = $coll->find($query,$fields);

$array= iterator_to_array($cursor);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

$cursor=$coll->find($query,$fields);

while($cursor->hasNext()){

$r=$cursor->getNext();

var_dump($r);

}

或者

$cursor=$coll->find($query,$fields);

$r=array();

foreache($cursoras$k=>$v){

var_dump($v);

$r[]=$v;

}

或者

$cursor=$coll->find($query,$fields);

$array=iterator_to_array($cursor);

快照MongoCursor::snapshot

保证一致性。在做find()操作时,获得$cursor之后,这个游标是动态的,在循环取结果过程中,如果有其他连接来更改符合条件的记录时,这个$cursor也会跟着变化的。$cursor->snapshot();之后,再插入或删除符合条件的记录时,获取的结果集将不再变化。如果是小于1M的结果集会自动被当作snapshot来处理。

如果要获取$cursor之后不变的结果需要这么做:

$cursor = $coll->find($query,$fields);

$cursor->snapshot();

1

2

$cursor=$coll->find($query,$fields);

$cursor->snapshot();

snapshot对findOne无效。

如需转载请注明出处:PHP操作MongoDB实例  http://www.ttlsa.com/html/2288.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值