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 “删除数据成功”;
}