這篇文章主要介紹了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" => "test@test.com"));$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();