|
復制代碼 代碼如下:
//連接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有用戶名密碼并指定數(shù)據(jù)庫blog
$conn = new Mongo("mongodb://${username}:${password}@localhost/blog");
//多個服務器
$conn = new Mongo("mongodb://localhost:27017,localhost:27018");
//選擇數(shù)據(jù)庫blog
$db = $conn->blog;
//制定結(jié)果集(表名: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);
//關閉數(shù)據(jù)庫
$conn->close();
php技術:php操作MongoDB基礎教程(連接、新增、修改、刪除、查詢),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。