PHP操做MongoDB實例

PHPmongodb擴展安裝 php

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 html

1
2
# vim /usr/local/php/etc/php.ini
extension = mongo . so

重啓php-fpm生效 git

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 github

鏈接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 web

PHP鏈接實例: mongodb

1
2
3
4
5
< ? 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 ) ) ;

在鏈接到複製集時,用它來判斷哪臺是primary。返回主機名稱、端口號、健康程度、狀態(1-primary,2-secondary,0-other)、ping耗時、前一次ping的時間戳。 數據庫

1
$ m -> getHosts ( ) ;

列出全部數據庫,返回數據庫名稱、大小、是否爲空以及總大小、ok狀態。 vim

1
$ m -> listDBs ( ) ;

選擇數據庫,返回數據庫對象 數組

1
2
3
$ db = $ m -> db_name ;
$ db = $ m -> selectDB ( db_name ) ;

選擇表(集合),返回文檔集合對象 php-fpm

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 ;

列出全部集合,返回集合對象

1
$ col_list = $ db -> listCollections ( ) ;

獲取當前選擇的數據庫名,返回數據庫名

1
$ db_name = $ db -> __toString ( ) ;

刪除當前數據庫

1
$ db -> drop ( ) ;

設置slaveok狀態(可讀狀態設置)

1
$ db -> setSlaveOkay ( true / false ) ;

獲取slaveok當前狀態

1
$ db -> getSlaveOkay ( ) ;

插入數據MongoCollection::insert(array $a,array $options)
array $a 要插入的數組
array $options 選項:safe 是否返回操做結果信息;fsync 是否直接插入到物理硬盤;w 寫入份數;timeout 超時時間

1
2
3
4
5
< ? php
$ 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 是否隻影響一條記錄

1
2
3
4
5
< ? php
$ 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 是否影響全部符合條件的記錄,默認隻影響一條

1
2
3
4
5
6
< ? php
$ 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 要得到的字段

1
2
3
4
5
< ? php
$ coll = $ m -> db_name -> col_name ;
$ query = array (score= > array ($ lt= > 10000 ) ) ;
$ fields = array (uid '=>true,’vip' = > true ) ;
$ rs = $ coll -> findOne ( $ query , $ fields ) ; 返回 arraynull

獲取多條記錄MongoCollection::find(array $query,array $fields)
array $query 條件
array $fields 要得到的字段

1
2
3
4
5
6
< ? php
$ 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
$ cursor = $ coll -> find ( ) ;
$ num = $ cursor -> count ( ) ;

選定列MongoCursor::fields

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

分頁

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

排序MongoCursor::sort

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

取查詢結果

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 ( $ cursor as $ 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以後不變的結果須要這麼作:

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

snapshot對findOne無效。

相關文章
相關標籤/搜索