在线地址:https://docs.qq.com/doc/DQXVNRnZXZ3NzY3ZP
在线图示: https://docs.qq.com/sheet/DQXNhbVRwRVpjdE12?tab=BB08J2&c=A1A0A0
<?php
//连接 mongodb 数据库
$manager = new MongoDB\Driver\Manager("mongodb://phpadmin:123456@localhost:27017/php");//1.插入操作
// 创建一个插入对象
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['name'=>'刘备','age'=>12,'email'=>'liubei@sohu.com']);
$bulk->insert(['name'=>'关羽','age'=>22,'email'=>'guanyu@sohu.com']);
$bulk->insert(['name'=>'张飞','age'=>18,'email'=>'zhangfei@sohu.com']);//执行插入操作
$manager->executeBulkWrite('php.stu',$bulk);
// echo 'ok';//2.读取数据
$filter = ['age'=>['$gt'=>16]];
$options = ['projection'=>['_id'=>0], #排除_id'sort'=>['age'=>1] #正序
];
// 创建一个查询对象
$query = new MongoDB\Driver\Query($filter, $options);
$data = $manager->executeQuery('php.stu',$query);foreach ($data as $v){print_r($v);
}//3.更新数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(['email' => 'guanyu@sohu.com'],['$set' => ['name' => '关羽','age'=>'22','email' => 'guanyu@sohu.com']],['multi' => false, 'upsert' => false]
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('php.stu', $bulk, $writeConcern);
echo 'ok';