PHP中操作MongoDB
阿新 • • 發佈:2018-12-24
<?php
//連結
$m = new Mongo();
// 選擇一個數據庫
$db = $m->mydb;
$collection = $db->stu;
//新增一個元素
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);
//修改
$newdata = array('$set' => array("title" => "Calvin and Hobbes"));
$collection->update(array("author" => "caleng"), $newdata);
//刪除
$collection->remove(array('author'=>'caleng'), array("justOne" => true));
//新增另一個元素,使用不同的格式
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);
//查詢所有的集合
$cursor = $collection->find();
//重複顯示結果
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
}
// 關閉連結
$m->close();
?>