下載相對應的php擴展文件php_mongo.dll,拷入php擴展文件夾裡/php/ext,修改php.ini文件,新增下列行: extension=php_mongo.dll PHP操作 連接數據庫 $conn = new Mongo("mongodb://localhost:27017//admin:admin"); 選擇數據庫和集合 復制代碼 //選擇數據庫blog,如果沒有,則創建 $db = $conn->yyd; //也可以寫成:$db = $conn->selectDB('yyd'); //制定結果集(集合:yyd_test) $collection = $db->yyd_test; //也可以寫成:$collection = $db->selectCollection('yyd_test'); //var_dump($collection); 復制代碼 新增數據 $post = array('name' => '22', 'sex' => '32'); $flag=($collection->insert($post)); var_dump($flag); image image 查找數據 復制代碼 $arr=array(); $cursor = $collection->find($arr); foreach($cursor as $key => $value){ echo "<pre>"; echo $value['_id']; echo '<br>name:'; echo $value['name']; echo "<br>sex:"; echo $value['sex']; echo "</pre>"; } 復制代碼 image 條件查找 $arr=array("name"=>"22"); $cursor = $collection->find($arr); 修改數據 image 復制代碼 $newdata = array('$set' => array("email" => "[email protected]")); $collection->update(array("name" => "22"), $newdata); var_dump($collection); $arr=array("name"=>"22"); $cursor = $collection->find($arr); foreach($cursor as $key => $value){ echo "<pre>"; echo $value['_id']; echo '<br>name:'; echo $value['name']; echo "<br>sex:"; echo $value['sex']; echo "<br>email:"; echo $value['email']; echo "</pre>"; } 復制代碼 刪除數據 image $arr=array("name"=>"22"); $collection->remove($arr); var_dump($collection); image 其它常用操作 復制代碼 //關閉連接 $conn->close(); //刪除一個數據庫 $conn->dropDB("yyd"); //列出所有可用數據庫 $dbs = $conn->listDBs();