php與mongoDB操作樣本詳解

來源:互聯網
上載者:User


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

Php代碼 

1. <?php  

2.  $m = new Mongo();      //這裡採用預設串連原生27017連接埠,當然你也可以串連遠程主機如                   192.168.0.4:27017,如果連接埠是27017,連接埠可以省略

3.   

4. $db = $m -> comedy;             // 選擇comedy資料庫,如果以前沒該資料庫會自動建立,也可以用$m->selectDB("comedy");  

5.   

6. $collection = $db->collection;  //選擇comedy裡面的collection集合,相當於RDBMS裡面的表,也-可以使用  

7. $db->selectCollection("collection");  

8.   

9. $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );  

10.   

11. $collection->insert($obj);     //將$obj 添加到$collection 集合中  

12.   

13.   

14. $obj = array( "title" => "XKCD", "online" => true );  

15. $collection->insert($obj);  

16. $cursor = $collection->find();  

17.   

18. foreach ($cursor as $obj) {    //遍曆所有集合中的文檔  

19. echo $obj["title"] . "n";  

20. }   

21.   

22.   

23. $m->close();          //斷開MongoDB串連  

 

下面在介紹一些常用的函數:

Php代碼 

1. $query = array( "i" => 71 );  

2. $cursor = $collection->find( $query );      // 在$collectio集合中尋找滿足$query的文檔  

3.   

4. while( $cursor->hasNext() ) {  

5. var_dump( $cursor->getNext() );  

6. }   

7.   

8. $collection -> findOne();            //返回$collection集合中第一個文檔  

9.   

10. $collection -> count();              //返回$collection集合中文檔的數量  

11. $coll->ensureIndex( array( "i" => 1 ) );  // 為i “這一列”加索引 降序排列  

12. $coll->ensureIndex( array( "i" => -1, "j" => 1 ) );  // 為i “這一列”加索引 降序排列 j升序  

 

查詢時,每個Object插入時都會自動產生一個獨特的_id,它相當於RDBMS中的主鍵,用於查詢時非常方便
如:

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. ?>  


更新時:

假如我們想修改下面文檔中comments中author的名字

Php代碼 

1. {   

2.     "_id" : ObjectId("4b06c282edb87a281e09dad9"),   

3.     "content" : "this is a blog post.",  

4.     "comments" :   

5.     [  

6.         {  

7.             "author" : "Mike",  

8.             "comment" : "I think that blah blah blah...",  

9.         },  

10.         {  

11.             "author" : "John",  

12.             "comment" : "I disagree."  

13.         }  

14.     ]  

15. }  

 

為了改變內部的一個域, 我們用 $set (保證文檔中其他域不被移除)並且comment的索引也變化

Php代碼 

1. <?php  

2.   

3. $collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 為要更新的元素  

4.   

5. ?>  


刪除一個資料庫

Php代碼 

1. $m -> dropDB("comedy");  

 

列出所有可用資料庫

Php代碼 

1. $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() 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.