国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php與mongoDB操作示例詳解

2024-05-04 21:49:08
字體:
供稿:網(wǎng)友

mongoDB數(shù)據(jù)庫是一種以json格式存儲(chǔ)的數(shù)據(jù)庫,非常適用于各種應(yīng)用開發(fā),下面我來給各位朋友介紹一些mongoDB學(xué)習(xí)實(shí)例.

mongodb想要整合PHP,需要安裝Mongo擴(kuò)展,這個(gè)比較簡(jiǎn)單,現(xiàn)在說一下MongoDB PHPAPI  及用法.

先看一個(gè)簡(jiǎn)單的例子,實(shí)例代碼如下:

  1. <?php   
  2.  
  3.  $m = new Mongo();  //這里采用默認(rèn)連接本機(jī)的27017端口,當(dāng)然你也可以連接遠(yuǎn)程主機(jī)如                   192.168.0.4:27017,如果端口是27017,端口可以省略 
  4.  
  5.  $db = $m -> comedy;  // 選擇comedy數(shù)據(jù)庫,如果以前沒該數(shù)據(jù)庫會(huì)自動(dòng)創(chuàng)建,也可以用$m->selectDB("comedy");   
  6. //開源代碼Vevb.com 
  7.  $collection = $db->collection;  //選擇comedy里面的collection集合,相當(dāng)于RDBMS里面的表,也-可以使用   
  8.  
  9.  $db->selectCollection("collection");   
  10.  
  11.  $obj = array"title" => "Calvin and Hobbes""author" => "Bill Watterson" );   
  12.     
  13.  $collection->insert($obj); //將$obj 添加到$collection 集合中    
  14.  
  15.  $obj = array"title" => "XKCD""online" => true );   
  16.  
  17.  $collection->insert($obj);   
  18.  
  19. $cursor = $collection->find();      
  20.  
  21.  foreach ($cursor as $obj) {  //遍歷所有集合中的文檔   
  22.  
  23.  echo $obj["title"] . "n";   
  24.  
  25.  }    
  26. $m->close();          //斷開MongoDB連接   

下面在介紹一些常用的函數(shù),Php代碼如下:

  1.  $query = array"i" => 71 );   
  2.  
  3.  $cursor = $collection->find( $query );      // 在$collectio集合中查找滿足$query的文檔   
  4.   
  5.  while$cursor->hasNext() ) {   
  6.  
  7.  var_dump( $cursor->getNext() );   
  8.  
  9.  }     
  10.  
  11.  $collection -> findOne();            //返回$collection集合中第一個(gè)文檔  
  12.    
  13.  $collection -> count();              //返回$collection集合中文檔的數(shù)量   
  14.  
  15.  $coll->ensureIndex( array"i" => 1 ) );  // 為i “這一列”加索引 降序排列   
  16.  
  17.  $coll->ensureIndex( array"i" => -1, "j" => 1 ) );  // 為i “這一列”加索引 降序排列 j升序   

查詢時(shí),每個(gè)Object插入時(shí)都會(huì)自動(dòng)生成一個(gè)獨(dú)特的_id,它相當(dāng)于RDBMS中的主鍵,用于查詢時(shí)非常方便,Php代碼如下:

  1. <?php  
  2.  
  3.  $person = array("name" => "joe");     
  4.  
  5.  $people->insert($person);     
  6.  
  7.   $joe = $people->findOne(array("_id" => $person['_id']));     
  8.  
  9.  ?> 

更新時(shí):假如我們想修改下面文檔中comments中author的名字,Php代碼如下: 

  1. {    
  2.  
  3.      "_id" : ObjectId("4b06c282edb87a281e09dad9"),    
  4.  
  5.      "content" : "this is a blog post.",   
  6.  
  7.      "comments" :    
  8.  
  9.      [   
  10.  
  11.          {   
  12.  
  13.              "author" : "Mike",   
  14.  
  15.              "comment" : "I think that blah blah blah...",   
  16.  
  17.          },   
  18.  
  19.          {   
  20.  
  21.              "author" : "John",   
  22.  
  23.              "comment" : "I disagree."   
  24.  
  25.          }   
  26.  
  27.      ]   
  28.  
  29.  }   

為了改變內(nèi)部的一個(gè)域,我們用 $set,保證文檔中其他域不被移除,并且comment的索引也變化,Php代碼如下:

  1. <?php    
  2.  
  3. $collection->update($criteriaarray('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 為要更新的元素     
  4.  
  5.  ?> 

刪除一個(gè)數(shù)據(jù)庫,Php代碼如下:

$m -> dropDB("comedy");  

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

$m->listDBs(); //無返回值  

好了就先寫這么多了,有興趣的話可以在網(wǎng)上搜到其他的關(guān)于Mongo-php API的用法.

命令行使用實(shí)例:

  1. 1. db.system.users.find()   
  2.  
  3. 2. db.users.count()   
  4.  
  5. 3. db.users.ensureIndex({password:-1})   
  6.  
  7. 4. use test   
  8.  
  9. 5. db.users.getIndexes()   
  10.  
  11. 6. db.repairDatabase()    
  12.  
  13. 7. show users   
  14.  
  15. 8. show dbs   
  16.  
  17. 9. db.users.find({username:{$in:['4d81a82398790']}}).explain()   
  18.  
  19. 10. db.users.dropIndexes()    
  20.  
  21. 11. db.users.find().count()   
  22.  
  23. 12. db.users.find().limit(5)   
  24.  
  25. 13. db.users.find({"username":"ssa"})     
  26.  
  27. 14. show collections    
  28.  
  29. 15. db.users.remove()   
  30.  
  31. 16. db.user.remove({'username':'admin'})   
  32.  
  33. 17. db.user.insert({'username':'admin','age':21,'nickname':'admin'})   
  34.  
  35. 18. db.user.save({'username':'admin','age':21,'info':['12','12313','zzsd']})   
  36.  
  37. 19. db.createCollection("user")     
  38.  
  39. 20. db.dropDatabase()    
  40.  
  41. 21. show collections    
  42.  
  43. 22. db.test.drop()    
  44.  
  45. 23. db.copyDatabase('test','test1')   
  46.  
  47. 24. show profile   
  48.  
  49. 25. db.printCollectionStats()   
  50.  
  51. 26. db.addUser('admin','admin123')     
  52.  
  53. 27. db.setProfilingLevel(2);   
  54.  
  55. 28. db.setProfilingLevel( 1 , 10 );   
  56.  
  57. 29. db.system.profile.find()

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永德县| 蒲江县| 夏河县| 彭阳县| 花垣县| 吉安市| 哈密市| 东乡| 久治县| 襄城县| 叙永县| 兴仁县| 宜城市| 保亭| 铁岭市| 兴国县| 宁德市| 绥宁县| 绥化市| 荆门市| 韶关市| 博乐市| 多伦县| 正定县| 嘉兴市| 长顺县| 德令哈市| 肇东市| 正宁县| 红安县| 大田县| 天长市| 珲春市| 丰顺县| 保定市| 右玉县| 兴海县| 宁化县| 平顺县| 察雅县| 德钦县|