Php 7.0下對MongoDB的操作
mogoDB的增刪改查操作
/**
mongo測試
@throws MongoCursorException
@throws MongoCursorTimeoutException
@throws MongoException
*/
public function mongotest(){
$bulk = new MongoDB\Driver\BulkWrite;
$document =array(
“id”=>uniqid(),
“name”=>“zq”,
“desc”=>“世界那麼大,我想出去看一看,天氣那麼冷,我就不信我會被凍 死。。。。。”,
“time”=>date(‘Y-m-d H-m-i’,time())
);
$_id= $bulk->insert($document); $manager = new MongoDB\Driver\Manager("mongodb://wxrecord:[email protected]:27017/wxrecord"); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); //執行插入操作 $result = $manager->executeBulkWrite('wxrecord.zq', $bulk, $writeConcern); echo "執行成功";
}
/**
mogodb插入資料庫操作
@author zq
*/
public function insert(){
$bulk = new MongoDB\Driver\BulkWrite;
$document =array(
“id”=>uniqid(),
“name”=>“zq”,
“desc”=>“世界那麼大,我想出去看一看,好女孩那麼多,我就不相信我找不到一個適合自己的咯?天氣那麼冷,我就不信我會被凍死。。。。。”,
“time”=>date(‘Y-m-d H-m-i’,time())
);
$_id=
document);
$manager = new MongoDB\Driver\Manager(“mongodb://localhost:27017”);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
//執行插入操作
$result = $manager->executeBulkWrite(‘zq.zq’, $bulk, $writeConcern);
echo “執行成功”;
}
/**
@throws \MongoDB\Driver\Exception\Exception
mongodb查詢資料庫操作
@author zq
/
public function search(){
$manager = new MongoDB\Driver\Manager(“mongodb://localhost:27017”);
$query = new MongoDB\Driver\Query(array(“name”=>“zq”));
$list = $manager->executeQuery(‘zq.zq’, $query);
list->toArray();
foreach($list as
value){
value;//將mongo物件強轉為陣列
key]=KaTeX parse error: Expected 'EOF', got '}' at position 15: value; }̲ print_r(list);
}
/*
mongodb更新資料庫操作
@author zq
*/
public function update(){
$bulk = new MongoDB\Driver\BulkWrite;
set’ => [‘name’ => ‘菜鳥工具’, ‘desc’ => ‘tool.runoob.com’]]
);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('zq.zq', $bulk, $writeConcern);
echo "更新資料成功";
}
/**
moogodb刪除資料操作
@author zq
*/
public function del(){
$bulk = new MongoDB\Driver\BulkWrite;
// $bulk->delete([‘x’ => 1], [‘limit’ => 1]); // limit 為 1 時,刪除第一條匹配資料
$bulk->delete([‘name’ => “菜鳥工具”], [‘limit’ => 0]); // limit 為 0 時,刪除所有匹配資料
$manager = new MongoDB\Driver\Manager(“mongodb://localhost:27017”);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite(‘zq.zq’, $bulk, $writeConcern);
echo “刪除資料成功”;
}