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:wxrecord@10.10.10.73: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=
b
u
l
k
−
>
i
n
s
e
r
t
(
bulk->insert(
bulk−>insert(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);
l
i
s
t
=
list=
list=list->toArray();
foreach($list as
k
e
y
=
>
key=>
key=>value){
v
a
l
u
e
=
(
a
r
r
a
y
)
value= (array)
value=(array)value;//将mongo对象强转为数组
l
i
s
t
[
list[
list[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;
b
u
l
k
−
>
u
p
d
a
t
e
(
[
′
n
a
m
e
′
=
>
"
z
q
"
]
,
[
′
bulk->update( ['name' => "zq"], ['
bulk−>update([′name′=>"zq"],[′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 “删除数据成功”;
}