這篇文章主要介紹了php操作MongoDB簡明教程,包括連接、新增、修改、刪除、查詢等,需要的朋友可以參考下
代碼如下: //連接localhost:27017 $conn = new Mongo(); //連接遠程主機默認端口 $conn = new Mongo('test.com'); //連接遠程主機22011端口 $conn = new Mongo('test.com:22011'); //MongoDB有用戶名密碼 $conn = new Mongo("mongodb://${username}:${password}@localhost") //MongoDB有用戶名密碼並指定數據庫blog $conn = new Mongo("mongodb://${username}:${password}@localhost/blog"); //多個服務器 $conn = new Mongo("mongodb://localhost:27017,localhost:27018"); //選擇數據庫blog $db = $conn->blog; //制定結果集(表名:users) $collection = $db->users; //新增 $user = array('name' => 'caleng', 'email' => 'admin#admin.com'); $collection->insert($user); //修改 $newdata = array('$set' => array("email" => "[email protected]")); $collection->update(array("name" => "caleng"), $newdata); //刪除 $collection->remove(array('name'=>'caleng'), array("justOne" => true)); //查找 $cursor = $collection->find(); var_dump($cursor); //查找一條 $user = $collection->findOne(array('name' => 'caleng'), array('email')); var_dump($user); //關閉數據庫 $conn->close();