码迷,mamicode.com
首页 > 数据库 > 详细

php7 安装mongodb4.0.4的扩展以及操作mongodb的方法

时间:2018-11-28 12:37:38      阅读:653      评论:0      收藏:0      [点我收藏+]

标签:upd   mongo   span   ISE   data   cursor   php   安装   let   

数据插入:

$manager = new MongoDB\Driver\Manager(‘mongodb://localhost:27017‘);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert([‘name‘ => ‘JetWu5‘, ‘age‘ => 26]);
$bulk->insert([‘name‘ => ‘JetWu6‘, ‘age‘ => 26]);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(‘wjt.friend‘, $bulk, $writeConcern);
echo ‘<pre>‘;
print_r($res);

数据查询:

$manager = new MongoDB\Driver\Manager(‘mongodb://localhost:27017‘);
$query = new MongoDB\Driver\Query([‘age‘ => 24], [‘sort‘ => [‘age‘ => 1]]);
$cursor = $manager->executeQuery(‘wjt.friend‘, $query);
$data = [];
foreach($cursor as $doc) {
  $data[] = $doc;
}
echo ‘<pre>‘;
print_r($data);

数据修改:

 
$manager = new MongoDB\Driver\Manager(‘mongodb://localhost:27017‘);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
  [‘name‘ => ‘JetWu5‘],
  [‘$set‘ => [‘age‘ => 30, ‘promise‘ => ‘always smile!‘]]
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(‘wjt.friend‘, $bulk, $writeConcern);
echo ‘<pre>‘;
print_r($res);

数据删除:

 
$manager = new MongoDB\Driver\Manager(‘mongodb://localhost:27017‘);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete([‘name‘ => ‘JetWu3‘]);
$bulk->delete([‘name‘ => ‘JetWu4‘]);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(‘wjt.friend‘, $bulk, $writeConcern);
echo ‘<pre>‘;
print_r($res);

 

php7 安装mongodb4.0.4的扩展以及操作mongodb的方法

标签:upd   mongo   span   ISE   data   cursor   php   安装   let   

原文地址:https://www.cnblogs.com/saintdingspage/p/10029996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!