1. 程式人生 > >PHP操作MongoDB

PHP操作MongoDB

1.連線MongoDB

$mongo = new Mongo();
$mongo = new Mongo("mongodb://username:[email protected]:12345");
$com = new Mongo("mongodb://admin:[email protected]:27017/test"); 


2.快速連線表

$com = new Mongo();
$table = $com->selectCollection('mydb','mytable');
$result = $table->find();


3.普通連線MongoDB
$mongo = new Mongo();
$db  = $mongo->mydb; //$db = $mongo->selectDB("mydb")
$table = $db->mytable; // $table = $db->selectCollection("mytable");

刪除資料
$db = $mo->dbname;
$db->drop();
獲取資料庫名字
$dbname = $db->_tostring()

4.Mongo插入 刪除 修改 查詢
查詢
$rsult = $table->find();
foreach($result as $val){
   print_r($val);
}

插入
$bool = $table->insert(array("age"=>100000));
var_dump($bool); //返回值為布林型別


$com = new Mongo("mongodb://tt:[email protected]
:27017/test"); $table = $com->selectCollection('test','mytable'); for( $i=1; $i < 10 ; $i++ ){     $table->insert(array("name"=>"name".$i,"age"=>$i+10));     } 修改 $bool = $table->update(array("name"=>"name2"),array("name"=>"name222")); var_dump($bool); //返回值為布林型別 刪除 $bool = $table->remove(array("name"=>"name1")); var_dump($bool); //返回值為布林型別