欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

深入PHP操作MongoDB的技術(shù)總結(jié)_PHP教程

編輯Tag賺U幣
教程Tag:phpmongodb添加

推薦:phpcms問答某模塊存儲型xss
黑名單惹的禍啊 在回帖處,有個(gè)編輯器功能。 編輯器存在一個(gè)可以編輯源碼,當(dāng)時(shí)第一反映就是。這里肯定存在xss。 果不其然,但是,在開始的時(shí)候測試 scriptalert(1)/script 以及 img src=1 onerror=alert(1) / 的時(shí)候,發(fā)現(xiàn)都無法執(zhí)行js代碼。 于是乎,便上http://html

復(fù)制代碼 代碼如下:m.dounai2.com

<?php
/**
* PHP操作MongoDB學(xué)習(xí)筆記
*/
//*************************
//** 連接MongoDB數(shù)據(jù)庫 **//
//*************************
//格式=>(“mongodb://用戶名:密碼 @地址:端口/默認(rèn)指定數(shù)據(jù)庫”,參數(shù))
$conn = new Mongo();
//可以簡寫為
//$conn=new Mongo(); #連接本地主機(jī),默認(rèn)端口.
//$conn=new Mongo(“172.21.15.69″); #連接遠(yuǎn)程主機(jī)
//$conn=new Mongo(“xiaocai.loc:10086″); #連接指定端口遠(yuǎn)程主機(jī)
//$conn=new Mongo(“xiaocai.loc”,array(“replicaSet”=>true)); #負(fù)載均衡
//$conn=new Mongo(“xiaocai.loc”,array(“persist”=>”t”)); #持久連接
//$conn=new Mongo(“mongodb://sa:123@localhost”); #帶用戶名密碼
//$conn=new Mongo(“mongodb://localhost:27017,localhost:27018″); #連接多個(gè)服務(wù)器
//$conn=new Mongo(“mongodb:///tmp/mongo-27017.sock”); #域套接字
//$conn=new Mongo(“mongodb://admin_miss:miss@localhost:27017/test”,array(‘persist'=>'p',”replicaSet”=>true)); #完整
//詳細(xì)資料:http://www.php.net/manual/en/mongo.connecting.php
//*************************
//** 選擇數(shù)據(jù)庫與表 **//
//*************************
$db=$conn->mydb; #選擇mydb數(shù)據(jù)庫
//$db=$conn->selectDB(“mydb”); #第二種寫法
$collection=$db->column; #選擇集合(選擇'表')
//$collection=$db->selectCollection(‘column'); #第二種寫法
//$collection=$conn->mydb->column; #更簡潔的寫法
//注意:1.數(shù)據(jù)庫和集合不需要事先創(chuàng)建,若它們不存在則會(huì)自動(dòng)創(chuàng)建它們.
// 2.注意錯(cuò)別字,你可能會(huì)無意間的創(chuàng)建一個(gè)新的數(shù)據(jù)庫(與原先的數(shù)據(jù)庫混亂).
//*************************
//** 插入文檔 **//
//*************************
//**向集合中插入數(shù)據(jù),返回bool判斷是否插入成功. **/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); #簡單插入
echo “新記錄ID:”.$array['_id']; #MongoDB會(huì)返回一個(gè)記錄標(biāo)識
var_dump($result); #返回:bool(true)
//**向集合中安全插入數(shù)據(jù),返回插入狀態(tài)(數(shù)組). **/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2′);
$result=$collection->insert($array,true); #用于等待MongoDB完成操作,以便確定是否成功.(當(dāng)有大量記錄插入時(shí)使用該參數(shù)會(huì)比較有用)
echo “新記錄ID:”.$array['_id']; #MongoDB會(huì)返回一個(gè)記錄標(biāo)識
var_dump($result); #返回:array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
//**完整的寫法 **/
#insert($array,array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))
/*
* *
* 完整格式:insert ( array $a [, array $options = array() ] )
* insert(array(),array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))
* 參數(shù):safe:默認(rèn)false,是否安全寫入
* fsync:默認(rèn)false,是否強(qiáng)制插入到同步到磁盤
* timeout:超時(shí)時(shí)間(毫秒)
*
* 插入結(jié)果:{ “_id” : ObjectId(“4d63552ad549a02c01000009″), “column_name” : “col770″, “column_exp” : “xiaocai” }
* '_id'為主鍵字段,在插入是MongoDB自動(dòng)添加.
*
* 注意:1.以下兩次插入的為同一條記錄(相同的_id),因?yàn)樗鼈兊闹迪嗤?BR>* $collection->insert(array(‘column_name'=>'xiaocai'));
* $collection->insert(array(‘column_name'=>'xiaocai'));
* 避免
* $collection->insert(array(‘column_name'=>'xiaocai'),true);
* try {
* $collection->insert(array(‘column_name'=>'xiaocai'),true);
* }catch(MongoCursorException $e){
* echo “Can't save the same person twice!\n”;
* }
*
* 詳細(xì)資料:http://www.php.net/manual/zh/mongocollection.insert.php
* *
*/
//*************************
//** 更新文檔 **//
//*************************
//** 修改更新 **/
$where=array(‘column_name'=>'col123′);
$newdata=array(‘column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array(‘$set'=>$newdata)); #$set:讓某節(jié)點(diǎn)等于給定值,類似的還有$pull $pullAll $pop $inc,在后面慢慢說明用法
/*
* 結(jié)果:
* 原數(shù)據(jù)
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col123″,”column_exp”:”xiaocai”}
* 被替換成了
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col123″,”column_exp”:”GGGGGGG”,”column_fid”:444}
*/
//** 替換更新 **/
$where=array(‘column_name'=>'col709′);
$newdata=array(‘column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);
/*
* 結(jié)果:
* 原數(shù)據(jù)
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col709″,”column_exp”:”xiaocai”}
* 被替換成了
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_exp”:”HHHHHHHHH”,”column_fid”:123}
*/
//** 批量更新 **/
$where=array(‘column_name'=>'col');
$newdata=array(‘column_exp'=>'multiple','91u'=>684435);
$result=$collection->update($where,array(‘$set'=>$newdata),array(‘multiple'=>true));
/**
* 所有'column_name'='col'都被修改
*/
//** 自動(dòng)累加 **/
$where=array('91u'=>684435);
$newdata=array(‘column_exp'=>'edit');
$result=$collection->update($where,array(‘$set'=>$newdata,'$inc'=>array('91u'=>-5)));
/**
* 更新91u=684435的數(shù)據(jù),并且91u自減5
*/
/** 刪除節(jié)點(diǎn) **/
$where=array(‘column_name'=>'col685′);
$result=$collection->update($where,array(‘$unset'=>'column_exp'));
/**
* 刪除節(jié)點(diǎn)column_exp
*/
/*
* *
* 完整格式:update(array $criteria, array $newobj [, array $options = array() ] )
* 注意:1.注意區(qū)分替換更新與修改更新
* 2.注意區(qū)分?jǐn)?shù)據(jù)類型如 array('91u'=>'684435′)與array('91u'=>684435)
* 詳細(xì)資料:http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
*/
//*************************
//** 刪除文檔 **//
//*************************
/** 清空數(shù)據(jù)庫 **/
$collection->remove(array(‘column_name'=>'col399′));
//$collection->remove(); #清空集合
/** 刪除指定MongoId **/
$id = new MongoId(“4d638ea1d549a02801000011″);
$collection->remove(array(‘_id'=>(object)$id));
/*
* *
* 使用下面的方法來匹配{“_id”:ObjectId(“4d638ea1d549a02801000011″)},查詢、更新也一樣
* $id = new MongoId(“4d638ea1d549a02801000011″);
* array(‘_id'=>(object)$id)
* *
*/
//*************************
//** 查詢文檔 **//
//*************************
/** 查詢文檔中的記錄數(shù) **/
echo ‘count:'.$collection->count().”<br>”; #全部
echo ‘count:'.$collection->count(array(‘type'=>'user')).”<br>”; #可以加上條件
echo ‘count:'.$collection->count(array(‘a(chǎn)ge'=>array(‘$gt'=>50,'$lte'=>74))).”<br>”; #大于50小于等于74
echo ‘count:'.$collection->find()->limit(5)->skip(0)->count(true).”<br>”; #獲得實(shí)際返回的結(jié)果數(shù)
/**
* 注:$gt為大于、$gte為大于等于、$lt為小于、$lte為小于等于、$ne為不等于、$exists不存在
*/
/** 集合中所有文檔 **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo “$id: “; var_dump($value); echo “<br>”;
}
/**
* 注意:
* 在我們做了find()操作,獲得$cursor游標(biāo)之后,這個(gè)游標(biāo)還是動(dòng)態(tài)的.
* 換句話說,在我find()之后,到我的游標(biāo)循環(huán)完成這段時(shí)間,如果再有符合條件的記錄被插入到collection,那么這些記錄也會(huì)被$cursor 獲得.
* 如果你想在獲得$cursor之后的結(jié)果集不變化,需要這樣做:
* $cursor = $collection->find();
* $cursor->snapshot();
* 詳見http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html
*/
/** 查詢一條數(shù)據(jù) **/
$cursor = $collection->findOne();
/**
* 注意:findOne()獲得結(jié)果集后不能使用snapshot(),fields()等函數(shù);
*/
/** age,type 列不顯示 **/
$cursor = $collection->find()->fields(array(“age”=>false,”type”=>false));
/** 只顯示user 列 **/
$cursor = $collection->find()->fields(array(“user”=>true));
/**
* 我這樣寫會(huì)出錯(cuò):$cursor->fields(array(“age”=>true,”type”=>false));
*/
/** (存在type,age節(jié)點(diǎn)) and age!=0 and age<50 **/
$where=array(‘type'=>array(‘$exists'=>true),'age'=>array(‘$ne'=>0,'$lt'=>50,'$exists'=>true));
$cursor = $collection->find($where);
/** 分頁獲取結(jié)果集 **/
$cursor = $collection->find()->limit(5)->skip(0);
/** 排序 **/
$cursor = $collection->find()->sort(array(‘a(chǎn)ge'=>-1,'type'=>1)); ##1表示降序 -1表示升序,參數(shù)的先后影響排序順序
/** 索引 **/
$collection->ensureIndex(array(‘a(chǎn)ge' => 1,'type'=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array(‘a(chǎn)ge' => 1,'type'=>-1),array(‘background'=>true)); #索引的創(chuàng)建放在后臺運(yùn)行(默認(rèn)是同步運(yùn)行)
$collection->ensureIndex(array(‘a(chǎn)ge' => 1,'type'=>-1),array(‘unique'=>true)); #該索引是唯一的
/**
* ensureIndex (array(),array(‘name'=>'索引名稱','background'=true,'unique'=true))
* 詳見:http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
/** 取得查詢結(jié)果 **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
//*************************
//** 文檔聚類 **//
//*************************
//這東西沒弄明白…
$conn->close(); #關(guān)閉連接
/*
關(guān)系型數(shù)據(jù)庫與MongoDB數(shù)據(jù)存儲的區(qū)別
MySql數(shù)據(jù)結(jié)構(gòu):
CREATE TABLE IF NOT EXISTS `column`(
`column_id` int(16) NOT NULL auto_increment COMMENT ‘主鍵',
`column_name` varchar(32) NOT NULL COMMENT ‘欄目名稱',
PRIMARY KEY (`column_id`)
);
CREATE TABLE IF NOT EXISTS `article`(
`article_id` int(16) NOT NULL auto_increment COMMENT ‘主鍵',
`article_caption` varchar(15) NOT NULL COMMENT ‘標(biāo)題',
PRIMARY KEY(`article_id`)
);
CREATE TABLE IF NOT EXISTS `article_body`(
`article_id` int(16) NOT NULL COMMENT ‘a(chǎn)rticle.article_id',
`body` text COMMENT ‘正文'
);
MongoDB數(shù)據(jù)結(jié)構(gòu):
$data=array(
‘column_name' =>'default',
‘a(chǎn)rticle' =>array(
‘a(chǎn)rticle_caption' => ‘xiaocai',
‘body' => ‘xxxxxxxxxx…'
)
);
$inc
如果記錄的該節(jié)點(diǎn)存在,讓該節(jié)點(diǎn)的數(shù)值加N;如果該節(jié)點(diǎn)不存在,讓該節(jié)點(diǎn)值等于N
設(shè)結(jié)構(gòu)記錄結(jié)構(gòu)為 array('a'=>1,'b'=>'t'),想讓a加5,那么:
$coll->update(
array('b'=>'t'),
array('$inc'=>array('a'=>5)),
)
$set
讓某節(jié)點(diǎn)等于給定值
設(shè)結(jié)構(gòu)記錄結(jié)構(gòu)為 array('a'=>1,'b'=>'t'),b為加f,那么:
$coll->update(
array('a'=>1),
array('$set'=>array('b'=>'f')),
)
$unset
刪除某節(jié)點(diǎn)
設(shè)記錄結(jié)構(gòu)為 array('a'=>1,'b'=>'t'),想刪除b節(jié)點(diǎn),那么:
$coll->update(
array('a'=>1),
array('$unset'=>'b'),
)
$push
如果對應(yīng)節(jié)點(diǎn)是個(gè)數(shù)組,就附加一個(gè)新的值上去;不存在,就創(chuàng)建這個(gè)數(shù)組,并附加一個(gè)值在這個(gè)數(shù)組上;如果該節(jié)點(diǎn)不是數(shù)組,返回錯(cuò)誤。
設(shè)記錄結(jié)構(gòu)為array('a'=>array(0=>'haha'),'b'=& gt;1),想附加新數(shù)據(jù)到節(jié)點(diǎn)a,那么:
$coll->update(
array('b'=>1),
array('$push'=>array('a'=>'wow')),
)
這樣,該記錄就會(huì)成為:array('a'=>array(0=>'haha',1=>'wow'),'b'=>1)
$pushAll
與$push類似,只是會(huì)一次附加多個(gè)數(shù)值到某節(jié)點(diǎn)
$addToSet
如果該階段的數(shù)組中沒有某值,就添加之
設(shè)記錄結(jié)構(gòu)為array('a'=>array(0=& gt;'haha'),'b'=>1),如果想附加新的數(shù)據(jù)到該節(jié)點(diǎn)a,那么:
$coll->update(
array('b'=>1),
array('$addToSet'=>array('a'=>'wow')),
)
如果在a節(jié)點(diǎn)中已經(jīng)有了wow,那么就不會(huì)再添加新的,如果沒有,就會(huì)為該節(jié)點(diǎn)添加新的item——wow。
$pop
設(shè)該記錄為array('a'=>array(0=>'haha',1=& gt;'wow'),'b'=>1)
刪除某數(shù)組節(jié)點(diǎn)的最后一個(gè)元素:
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>1)),
)
刪除某數(shù)組階段的第一個(gè)元素
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>-1)),
)
$pull
如果該節(jié)點(diǎn)是個(gè)數(shù)組,那么刪除其值為value的子項(xiàng),如果不是數(shù)組,會(huì)返回一個(gè)錯(cuò)誤。
設(shè)該記錄為 array('a'=>array(0=>'haha',1=>'wow'),'b'=>1),想要?jiǎng)h除a中value為 haha的子項(xiàng):
$coll->update(
array('b'=>1),
array('$pull=>array('a'=>'haha')),
)
結(jié)果為: array('a'=>array(0=>'wow'),'b'=>1)
$pullAll
與$pull類似,只是可以刪除一組符合條件的記錄。
*/
?>

復(fù)制代碼 代碼如下:m.dounai2.com

<?php
/**
* PHP操作MongoDB學(xué)習(xí)筆記
*/
//*************************
//** 連接MongoDB數(shù)據(jù)庫 **//
//*************************
//格式=>(“mongodb://用戶名:密碼 @地址:端口/默認(rèn)指定數(shù)據(jù)庫”,參數(shù))
$conn = new Mongo();
//可以簡寫為
//$conn=new Mongo(); #連接本地主機(jī),默認(rèn)端口.
//$conn=new Mongo(“172.21.15.69″); #連接遠(yuǎn)程主機(jī)
//$conn=new Mongo(“xiaocai.loc:10086″); #連接指定端口遠(yuǎn)程主機(jī)
//$conn=new Mongo(“xiaocai.loc”,array(“replicaSet”=>true)); #負(fù)載均衡
//$conn=new Mongo(“xiaocai.loc”,array(“persist”=>”t”)); #持久連接
//$conn=new Mongo(“mongodb://sa:123@localhost”); #帶用戶名密碼
//$conn=new Mongo(“mongodb://localhost:27017,localhost:27018″); #連接多個(gè)服務(wù)器
//$conn=new Mongo(“mongodb:///tmp/mongo-27017.sock”); #域套接字
//$conn=new Mongo(“mongodb://admin_miss:miss@localhost:27017/test”,array(‘persist'=>'p',”replicaSet”=>true)); #完整
//詳細(xì)資料:http://www.php.net/manual/en/mongo.connecting.php
//*************************
//** 選擇數(shù)據(jù)庫與表 **//
//*************************
$db=$conn->mydb; #選擇mydb數(shù)據(jù)庫
//$db=$conn->selectDB(“mydb”); #第二種寫法
$collection=$db->column; #選擇集合(選擇'表')
//$collection=$db->selectCollection(‘column'); #第二種寫法
//$collection=$conn->mydb->column; #更簡潔的寫法
//注意:1.數(shù)據(jù)庫和集合不需要事先創(chuàng)建,若它們不存在則會(huì)自動(dòng)創(chuàng)建它們.
// 2.注意錯(cuò)別字,你可能會(huì)無意間的創(chuàng)建一個(gè)新的數(shù)據(jù)庫(與原先的數(shù)據(jù)庫混亂).
//*************************
//** 插入文檔 **//
//*************************
//**向集合中插入數(shù)據(jù),返回bool判斷是否插入成功. **/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai');
$result=$collection->insert($array); #簡單插入
echo “新記錄ID:”.$array['_id']; #MongoDB會(huì)返回一個(gè)記錄標(biāo)識
var_dump($result); #返回:bool(true)
//**向集合中安全插入數(shù)據(jù),返回插入狀態(tài)(數(shù)組). **/
$array=array(‘column_name'=>'col'.rand(100,999),'column_exp'=>'xiaocai2′);
$result=$collection->insert($array,true); #用于等待MongoDB完成操作,以便確定是否成功.(當(dāng)有大量記錄插入時(shí)使用該參數(shù)會(huì)比較有用)
echo “新記錄ID:”.$array['_id']; #MongoDB會(huì)返回一個(gè)記錄標(biāo)識
var_dump($result); #返回:array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
//**完整的寫法 **/
#insert($array,array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))
/*
* *
* 完整格式:insert ( array $a [, array $options = array() ] )
* insert(array(),array(‘safe'=>false,'fsync'=>false,'timeout'=>10000))
* 參數(shù):safe:默認(rèn)false,是否安全寫入
* fsync:默認(rèn)false,是否強(qiáng)制插入到同步到磁盤
* timeout:超時(shí)時(shí)間(毫秒)
*
* 插入結(jié)果:{ “_id” : ObjectId(“4d63552ad549a02c01000009″), “column_name” : “col770″, “column_exp” : “xiaocai” }
* '_id'為主鍵字段,在插入是MongoDB自動(dòng)添加.
*
* 注意:1.以下兩次插入的為同一條記錄(相同的_id),因?yàn)樗鼈兊闹迪嗤?BR>* $collection->insert(array(‘column_name'=>'xiaocai'));
* $collection->insert(array(‘column_name'=>'xiaocai'));
* 避免
* $collection->insert(array(‘column_name'=>'xiaocai'),true);
* try {
* $collection->insert(array(‘column_name'=>'xiaocai'),true);
* }catch(MongoCursorException $e){
* echo “Can't save the same person twice!\n”;
* }
*
* 詳細(xì)資料:http://www.php.net/manual/zh/mongocollection.insert.php
* *
*/
//*************************
//** 更新文檔 **//
//*************************
//** 修改更新 **/
$where=array(‘column_name'=>'col123′);
$newdata=array(‘column_exp'=>'GGGGGGG','column_fid'=>444);
$result=$collection->update($where,array(‘$set'=>$newdata)); #$set:讓某節(jié)點(diǎn)等于給定值,類似的還有$pull $pullAll $pop $inc,在后面慢慢說明用法
/*
* 結(jié)果:
* 原數(shù)據(jù)
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col123″,”column_exp”:”xiaocai”}
* 被替換成了
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col123″,”column_exp”:”GGGGGGG”,”column_fid”:444}
*/
//** 替換更新 **/
$where=array(‘column_name'=>'col709′);
$newdata=array(‘column_exp'=>'HHHHHHHHH','column_fid'=>123);
$result=$collection->update($where,$newdata);
/*
* 結(jié)果:
* 原數(shù)據(jù)
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col709″,”column_exp”:”xiaocai”}
* 被替換成了
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_exp”:”HHHHHHHHH”,”column_fid”:123}
*/
//** 批量更新 **/
$where=array(‘column_name'=>'col');
$newdata=array(‘column_exp'=>'multiple','91u'=>684435);
$result=$collection->update($where,array(‘$set'=>$newdata),array(‘multiple'=>true));
/**
* 所有'column_name'='col'都被修改
*/
//** 自動(dòng)累加 **/
$where=array('91u'=>684435);
$newdata=array(‘column_exp'=>'edit');
$result=$collection->update($where,array(‘$set'=>$newdata,'$inc'=>array('91u'=>-5)));
/**
* 更新91u=684435的數(shù)據(jù),并且91u自減5
*/
/** 刪除節(jié)點(diǎn) **/
$where=array(‘column_name'=>'col685′);
$result=$collection->update($where,array(‘$unset'=>'column_exp'));
/**
* 刪除節(jié)點(diǎn)column_exp
*/
/*
* *
* 完整格式:update(array $criteria, array $newobj [, array $options = array() ] )
* 注意:1.注意區(qū)分替換更新與修改更新
* 2.注意區(qū)分?jǐn)?shù)據(jù)類型如 array('91u'=>'684435′)與array('91u'=>684435)
* 詳細(xì)資料:http://www.mongodb.org/display/DOCS/Updating#Updating-%24bit
* *
*/
//*************************
//** 刪除文檔 **//
//*************************
/** 清空數(shù)據(jù)庫 **/
$collection->remove(array(‘column_name'=>'col399′));
//$collection->remove(); #清空集合
/** 刪除指定MongoId **/
$id = new MongoId(“4d638ea1d549a02801000011″);
$collection->remove(array(‘_id'=>(object)$id));
/*
* *
* 使用下面的方法來匹配{“_id”:ObjectId(“4d638ea1d549a02801000011″)},查詢、更新也一樣
* $id = new MongoId(“4d638ea1d549a02801000011″);
* array(‘_id'=>(object)$id)
* *
*/
//*************************
//** 查詢文檔 **//
//*************************
/** 查詢文檔中的記錄數(shù) **/
echo ‘count:'.$collection->count().”<br>”; #全部
echo ‘count:'.$collection->count(array(‘type'=>'user')).”<br>”; #可以加上條件
echo ‘count:'.$collection->count(array(‘a(chǎn)ge'=>array(‘$gt'=>50,'$lte'=>74))).”<br>”; #大于50小于等于74
echo ‘count:'.$collection->find()->limit(5)->skip(0)->count(true).”<br>”; #獲得實(shí)際返回的結(jié)果數(shù)
/**
* 注:$gt為大于、$gte為大于等于、$lt為小于、$lte為小于等于、$ne為不等于、$exists不存在
*/
/** 集合中所有文檔 **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo “$id: “; var_dump($value); echo “<br>”;
}
/**
* 注意:
* 在我們做了find()操作,獲得$cursor游標(biāo)之后,這個(gè)游標(biāo)還是動(dòng)態(tài)的.
* 換句話說,在我find()之后,到我的游標(biāo)循環(huán)完成這段時(shí)間,如果再有符合條件的記錄被插入到collection,那么這些記錄也會(huì)被$cursor 獲得.
* 如果你想在獲得$cursor之后的結(jié)果集不變化,需要這樣做:
* $cursor = $collection->find();
* $cursor->snapshot();
* 詳見http://www.bumao.com/index.php/2010/08/mongo_php_cursor.html
*/
/** 查詢一條數(shù)據(jù) **/
$cursor = $collection->findOne();
/**
* 注意:findOne()獲得結(jié)果集后不能使用snapshot(),fields()等函數(shù);
*/
/** age,type 列不顯示 **/
$cursor = $collection->find()->fields(array(“age”=>false,”type”=>false));
/** 只顯示user 列 **/
$cursor = $collection->find()->fields(array(“user”=>true));
/**
* 我這樣寫會(huì)出錯(cuò):$cursor->fields(array(“age”=>true,”type”=>false));
*/
/** (存在type,age節(jié)點(diǎn)) and age!=0 and age<50 **/
$where=array(‘type'=>array(‘$exists'=>true),'age'=>array(‘$ne'=>0,'$lt'=>50,'$exists'=>true));
$cursor = $collection->find($where);
/** 分頁獲取結(jié)果集 **/
$cursor = $collection->find()->limit(5)->skip(0);
/** 排序 **/
$cursor = $collection->find()->sort(array(‘a(chǎn)ge'=>-1,'type'=>1)); ##1表示降序 -1表示升序,參數(shù)的先后影響排序順序
/** 索引 **/
$collection->ensureIndex(array(‘a(chǎn)ge' => 1,'type'=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array(‘a(chǎn)ge' => 1,'type'=>-1),array(‘background'=>true)); #索引的創(chuàng)建放在后臺運(yùn)行(默認(rèn)是同步運(yùn)行)
$collection->ensureIndex(array(‘a(chǎn)ge' => 1,'type'=>-1),array(‘unique'=>true)); #該索引是唯一的
/**
* ensureIndex (array(),array(‘name'=>'索引名稱','background'=true,'unique'=true))
* 詳見:http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
/** 取得查詢結(jié)果 **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
//*************************
//** 文檔聚類 **//
//*************************
//這東西沒弄明白…
$conn->close(); #關(guān)閉連接
/*
關(guān)系型數(shù)據(jù)庫與MongoDB數(shù)據(jù)存儲的區(qū)別
MySql數(shù)據(jù) 結(jié)構(gòu):
CREATE TABLE IF NOT EXISTS `column`(
`column_id` int(16) NOT NULL auto_increment COMMENT ‘主鍵',
`column_name` varchar(32) NOT NULL COMMENT ‘欄目名稱',
PRIMARY KEY (`column_id`)
);
CREATE TABLE IF NOT EXISTS `article`(
`article_id` int(16) NOT NULL auto_increment COMMENT ‘主鍵',
`article_caption` varchar(15) NOT NULL COMMENT ‘標(biāo)題',
PRIMARY KEY(`article_id`)
);
CREATE TABLE IF NOT EXISTS `article_body`(
`article_id` int(16) NOT NULL COMMENT ‘a(chǎn)rticle.article_id',
`body` text COMMENT ‘正文'
);
MongoDB數(shù)據(jù)結(jié)構(gòu):
$data=array(
‘column_name' =>'default',
‘a(chǎn)rticle' =>array(
‘a(chǎn)rticle_caption' => ‘xiaocai',
‘body' => ‘xxxxxxxxxx…'
)
);
$inc
如果記錄的該節(jié)點(diǎn)存在,讓該節(jié)點(diǎn)的數(shù)值加N;如果該節(jié)點(diǎn)不存在,讓該節(jié)點(diǎn)值等 于N
設(shè)結(jié)構(gòu)記錄結(jié)構(gòu)為 array('a'=>1,'b'=>'t'),想讓a加5,那么:
$coll->update(
array('b'=>'t'),
array('$inc'=>array('a'=>5)),
)
$set
讓某節(jié)點(diǎn)等于給定值
設(shè)結(jié)構(gòu)記錄結(jié)構(gòu)為 array('a'=>1,'b'=>'t'),b為加f,那么:
$coll->update(
array('a'=>1),
array('$set'=>array('b'=>'f')),
)
$unset
刪除某節(jié)點(diǎn)
設(shè)記錄結(jié)構(gòu)為 array('a'=>1,'b'=>'t'),想刪除b節(jié)點(diǎn),那么:
$coll->update(
array('a'=>1),
array('$unset'=>'b'),
)
$push
如果對應(yīng)節(jié)點(diǎn)是個(gè)數(shù)組,就附加一個(gè)新的值上去;不存在,就創(chuàng)建這個(gè)數(shù)組,并附加一個(gè)值在這個(gè)數(shù)組上;如果 該節(jié)點(diǎn)不是數(shù)組,返回錯(cuò)誤。
設(shè)記錄結(jié)構(gòu)為array('a'=>array(0=>'haha'),'b'=& gt;1),想附加新數(shù)據(jù)到節(jié)點(diǎn)a,那么:
$coll->update(
array('b'=>1),
array('$push'=>array('a'=>'wow')),
)
這 樣,該記錄就會(huì)成為:array('a'=>array(0=>'haha',1=>'wow'),'b'=>1)
$pushAll
與$push類似,只是會(huì)一次附加多個(gè)數(shù)值到某節(jié)點(diǎn)
$addToSet
如果該階段的數(shù)組中沒有某值,就添加之
設(shè)記錄結(jié)構(gòu)為array('a'=>array(0=& gt;'haha'),'b'=>1),如果想附加新的數(shù)據(jù)到該節(jié)點(diǎn)a,那么:
$coll->update(
array('b'=>1),
array('$addToSet'=>array('a'=>'wow')),
)
如果在a節(jié)點(diǎn)中已經(jīng)有了wow,那么就不會(huì)再添加新的,如果沒有,就會(huì)為該節(jié)點(diǎn)添加新的item——wow。
$pop
設(shè)該記錄為array('a'=>array(0=>'haha',1=& gt;'wow'),'b'=>1)
刪除某數(shù)組節(jié)點(diǎn)的最后一個(gè)元素:
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>1)),
)
刪除某數(shù)組階段的第一個(gè)元素
$coll->update(
array('b'=>1),
array('$pop=>array('a'=>-1)),
)
$pull
如果該節(jié)點(diǎn)是個(gè)數(shù)組,那么刪除其值為value的子項(xiàng),如果不是數(shù)組,會(huì)返回一個(gè)錯(cuò)誤。
設(shè)該記錄為 array('a'=>array(0=>'haha',1=>'wow'),'b'=>1),想要?jiǎng)h除a中value為 haha的子項(xiàng):
$coll->update(
array('b'=>1),
array('$pull=>array('a'=>'haha')),
)
結(jié) 果為: array('a'=>array(0=>'wow'),'b'=>1)
$pullAll
與$pull類似,只是可以刪除一組符合條件的記錄。
*/
?>

分享:PHP學(xué)習(xí)筆記之面向?qū)ο笤O(shè)計(jì)
面向?qū)ο笤O(shè)計(jì)是php程序開發(fā)中一個(gè)很重要的內(nèi)容塊,如果你想成為高級php程序員我們必須知道面向?qū)ο笤O(shè)計(jì)具體用法與寫法。 維護(hù)簡單 模塊化是面向?qū)ο缶幊讨械囊粋(gè)特征。實(shí)體被表示為類和同一名字空間中具有相同功能的類,我們可以在名字空間中添加一個(gè)類而不會(huì)影響該名

來源:模板無憂//所屬分類:PHP教程/更新時(shí)間:2013-06-03
相關(guān)PHP教程