php操作mongoDB實(shí)例分析
本文實(shí)例講述了php操作mongoDB的方法。分享給大家供大家參考。具體分析如下:
mongoDB數(shù)據(jù)庫(kù)是一種以json格式存儲(chǔ)的數(shù)據(jù)庫(kù),非常適用于各種應(yīng)用開(kāi)發(fā),這里就來(lái)給各位朋友介紹一些mongoDB學(xué)習(xí)實(shí)例.
mongodb想要整合PHP,需要安裝Mongo擴(kuò)展,這個(gè)比較簡(jiǎn)單,現(xiàn)在說(shuō)一下MongoDB PHPAPI 及用法.
先看一個(gè)簡(jiǎn)單的例子,實(shí)例代碼如下:
$m = new Mongo(); //這里采用默認(rèn)連接本機(jī)的27017端口,當(dāng)然你也可以連接遠(yuǎn)程主機(jī)如 192.168.0.4:27017,如果端口是27017,端口可以省略
$db = $m -> comedy; // 選擇comedy數(shù)據(jù)庫(kù),如果以前沒(méi)該數(shù)據(jù)庫(kù)會(huì)自動(dòng)創(chuàng)建,也可以用$m->selectDB("comedy");
$collection = $db->collection; //選擇comedy里面的collection集合,相當(dāng)于RDBMS里面的表,也-可以使用
$db->selectCollection("collection");
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj); //將$obj 添加到$collection 集合中
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);
$cursor = $collection->find();
foreach ($cursor as $obj) { //遍歷所有集合中的文檔
echo $obj["title"] . "n";
}
$m->close(); //斷開(kāi)MongoDB連接
下面在介紹一些常用的函數(shù),Php代碼如下:
$cursor = $collection->find( $query );// 在$collectio集合中查找滿足$query的文檔
while( $cursor->hasNext() ) {
var_dump( $cursor->getNext() );
}
$collection -> findOne();//返回$collection集合中第一個(gè)文檔
$collection -> count(); //返回$collection集合中文檔的數(shù)量
$coll->ensureIndex( array( "i" => 1 ) ); // 為i “這一列”加索引 降序排列
$coll->ensureIndex( array( "i" => -1, "j" => 1 ) ); // 為i “這一列”加索引 降序排列 j升序
查詢時(shí),每個(gè)Object插入時(shí)都會(huì)自動(dòng)生成一個(gè)獨(dú)特的_id,它相當(dāng)于RDBMS中的主鍵,用于查詢時(shí)非常方便,Php代碼如下:
$person = array("name" => "joe");
$people->insert($person);
$joe = $people->findOne(array("_id" => $person['_id']));
?>
更新時(shí):假如我們想修改下面文檔中comments中author的名字,Php代碼如下:
"_id" : ObjectId("4b06c282edb87a281e09dad9"),
"content" : "this is a blog post.",
"comments" :
[
{
"author" : "Mike",
"comment" : "I think that blah blah blah...",
},
{
"author" : "John",
"comment" : "I disagree."
}
]
}
為了改變內(nèi)部的一個(gè)域,我們用 $set,保證文檔中其他域不被移除,并且comment的索引也變化,Php代碼如下:
$collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 為要更新的元素
?>
刪除一個(gè)數(shù)據(jù)庫(kù),Php代碼如下:
列出所有可用數(shù)據(jù)庫(kù),Php代碼如下:
好了就先寫(xiě)這么多了,有興趣的話可以在網(wǎng)上搜到其他的關(guān)于Mongo-php API的用法.
命令行使用實(shí)例:
1. db.system.users.find()
2. db.users.count()
3. db.users.ensureIndex({password:-1})
4. use test
5. db.users.getIndexes()
6. db.repairDatabase()
7. show users
8. show dbs
9. db.users.find({username:{$in:['4d81a82398790']}}).explain()
10. db.users.dropIndexes()
11. db.users.find().count()
12. db.users.find().limit(5)
13. db.users.find({"username":"ssa"})
14. show collections
15. db.users.remove()
16. db.user.remove({'username':'admin'})
17. db.user.insert({'username':'admin','age':21,'nickname':'admin'})
18. db.user.save({'username':'admin','age':21,'info':['12','12313','zzsd']})
19. db.createCollection("user")
20. db.dropDatabase()
21. show collections
22. db.test.drop()
23. db.copyDatabase('test','test1')
24. show profile
25. db.printCollectionStats()
26. db.addUser('admin','admin123')
27. db.setProfilingLevel(2);
28. db.setProfilingLevel( 1 , 10 );
29. db.system.profile.find()
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
在mysql數(shù)據(jù)庫(kù)原有字段后增加新內(nèi)容
在mysql數(shù)據(jù)庫(kù)原有字段后增加新內(nèi)容2009-11-11php基于socket實(shí)現(xiàn)SMTP發(fā)送郵件的方法
這篇文章主要介紹了php基于socket實(shí)現(xiàn)SMTP發(fā)送郵件的方法,實(shí)例分析了php采用socket實(shí)現(xiàn)smtp發(fā)送郵件的原理與技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03PHP+MySQL實(shí)現(xiàn)輸入頁(yè)碼跳轉(zhuǎn)到指定頁(yè)面功能示例
這篇文章主要介紹了PHP+MySQL實(shí)現(xiàn)輸入頁(yè)碼跳轉(zhuǎn)到指定頁(yè)面功能,結(jié)合實(shí)例形式分析了php連接mysql數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)查詢及分頁(yè)顯示、指定頁(yè)數(shù)跳轉(zhuǎn)顯示等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06PHP調(diào)用ffmpeg對(duì)視頻截圖并拼接腳本
這篇文章主要介紹了PHP調(diào)用ffmpeg對(duì)視頻截圖并拼接腳本2018-01-01PHP nl2br函數(shù) 將換行字符轉(zhuǎn)成 <br>
PHP nl2br函數(shù) 將換行字符轉(zhuǎn)成 <br>,不是很了解的朋友可以參考下。2009-08-08PHP實(shí)現(xiàn)的mysql讀寫(xiě)分離操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的mysql讀寫(xiě)分離操作,簡(jiǎn)單講述了mysql讀寫(xiě)分離的原理,并結(jié)合實(shí)例形式給出了php針對(duì)mysql的讀寫(xiě)sql語(yǔ)句操作不同數(shù)據(jù)庫(kù)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05