|
復(fù)制代碼 代碼如下:
//連接localhost:27017
$conn = new Mongo();
//連接遠(yuǎn)程主機(jī)默認(rèn)端口
$conn = new Mongo('test.com');
//連接遠(yuǎn)程主機(jī)22011端口
$conn = new Mongo('test.com:22011');
//MongoDB有用戶名密碼
$conn = new Mongo("mongodb://${username}:${password}@localhost")
//MongoDB有用戶名密碼并指定數(shù)據(jù)庫(kù)blog
$conn = new Mongo("mongodb://${username}:${password}@localhost/blog");
//多個(gè)服務(wù)器
$conn = new Mongo("mongodb://localhost:27017,localhost:27018");
//選擇數(shù)據(jù)庫(kù)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);
//關(guān)閉數(shù)據(jù)庫(kù)
$conn->close();
php技術(shù):php操作MongoDB基礎(chǔ)教程(連接、新增、修改、刪除、查詢),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。