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

php操作mongoDB實例分析

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

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

mongoDB數(shù)據(jù)庫是一種以json格式存儲的數(shù)據(jù)庫,非常適用于各種應(yīng)用開發(fā),這里就來給各位朋友介紹一些mongoDB學習實例.

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

先看一個簡單的例子,實例代碼如下:

復(fù)制代碼 代碼如下:
<?php
 $m = new Mongo();  //這里采用默認連接本機的27017端口,當然你也可以連接遠程主機如 192.168.0.4:27017,如果端口是27017,端口可以省略
 $db = $m -> comedy;  // 選擇comedy數(shù)據(jù)庫,如果以前沒該數(shù)據(jù)庫會自動創(chuàng)建,也可以用$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連接

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

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

更新時:假如我們想修改下面文檔中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)部的一個域,我們用 $set,保證文檔中其他域不被移除,并且comment的索引也變化,Php代碼如下:
復(fù)制代碼 代碼如下:
<?php
$collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 為要更新的元素 
?>

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

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

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

好了就先寫這么多了,有興趣的話可以在網(wǎng)上搜到其他的關(guān)于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程序設(shè)計有所幫助。

相關(guān)文章

最新評論