php操作mongoDB執行個體分析_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php操作mongoDB的方法。分享給大家供大家參考。具體分析如下:

mongoDB資料庫是一種以json格式儲存的資料庫,非常適用於各種應用開發,這裡就來給各位朋友介紹一些mongoDB學習執行個體.

mongodb想要整合PHP,需要安裝Mongo擴充,這個比較簡單,現在說一下MongoDB PHPAPI  及用法.

先看一個簡單的例子,執行個體代碼如下:

複製代碼 代碼如下:
<?php
 $m = new Mongo();  //這裡採用預設串連原生27017連接埠,當然你也可以串連遠程主機如 192.168.0.4:27017,如果連接埠是27017,連接埠可以省略
 $db = $m -> comedy;  // 選擇comedy資料庫,如果以前沒該資料庫會自動建立,也可以用$m->selectDB("comedy");
 $collection = $db->collection;  //選擇comedy裡面的collection集合,相當於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(); //斷開MongoDB串連

下面在介紹一些常用的函數,Php代碼如下:
複製代碼 代碼如下:
$query = array( "i" => 71 );
 $cursor = $collection->find( $query );// 在$collectio集合中尋找滿足$query的文檔
 while( $cursor->hasNext() ) {
 var_dump( $cursor->getNext() );
 } 
 
 $collection -> findOne();//返回$collection集合中第一個文檔 
 $collection -> count();  //返回$collection集合中文檔的數量 
 $coll->ensureIndex( array( "i" => 1 ) );  // 為i “這一列”加索引 降序排列
 $coll->ensureIndex( array( "i" => -1, "j" => 1 ) );  // 為i “這一列”加索引 降序排列 j升序

查詢時,每個Object插入時都會自動產生一個獨特的_id,它相當於RDBMS中的主鍵,用於查詢時非常方便,Php代碼如下:
複製代碼 代碼如下:
<?php 
 $person = array("name" => "joe"); 
 $people->insert($person); 
  $joe = $people->findOne(array("_id" => $person['_id'])); 
?>

更新時:假如我們想修改下面文檔中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."
         }
     ]
}

為了改變內部的一個域,我們用 $set,保證文檔中其他域不被移除,並且comment的索引也變化,Php代碼如下:
複製代碼 代碼如下:
<?php
$collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 為要更新的元素 
?>

刪除一個資料庫,Php代碼如下:
複製代碼 代碼如下:
$m -> dropDB("comedy");

列出所有可用資料庫,Php代碼如下:

複製代碼 代碼如下:
$m->listDBs(); //無傳回值

好了就先寫這麼多了,有興趣的話可以在網上搜到其他的關於Mongo-php API的用法.

命令列使用執行個體:

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()

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.