欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php操作mongoDB實(shí)例分析

 更新時(shí)間:2014年12月29日 11:18:28   投稿:shichen2014  
這篇文章主要介紹了php操作mongoDB的方法,實(shí)例分析了php操作mongoDB常用的各類技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(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í)例代碼如下:

復(fù)制代碼 代碼如下:
<?php
 $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代碼如下:
復(fù)制代碼 代碼如下:
$query = array( "i" => 71 );
 $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代碼如下:
復(fù)制代碼 代碼如下:
<?php 
 $person = array("name" => "joe"); 
 $people->insert($person); 
  $joe = $people->findOne(array("_id" => $person['_id'])); 
?>

更新時(shí):假如我們想修改下面文檔中comments中author的名字,Php代碼如下:
復(fù)制代碼 代碼如下:
{
     "_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代碼如下:
復(fù)制代碼 代碼如下:
<?php
$collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 為要更新的元素 
?>

刪除一個(gè)數(shù)據(jù)庫(kù),Php代碼如下:
復(fù)制代碼 代碼如下:
$m -> dropDB("comedy");

列出所有可用數(shù)據(jù)庫(kù),Php代碼如下:

復(fù)制代碼 代碼如下:
$m->listDBs(); //無(wú)返回值

好了就先寫(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)文章

最新評(píng)論