php7的mongodb基本用法(代码详解)

互联网 19-4-18

提示:PHP的mongodb扩展,从5.6开始已经废弃了原来的mongo扩展,PHP7的pecl下载地址:https://pecl.php.net/package/mongodb,或者选择mongodb官方开发:https://github.com/mongodb/mongo-php-library,git上的这个支持以前mongo写法,pecl上的mongodb不支持以前的写法。

下面用PHP7新的API总结一下:

一:CURD

1:链接

<?php //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:123@10.10.10.104:27017');

root 用户;123:密码;如果没有密码则不写

2:查询

<?php  //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017');  //查询 $filter =  ['user_id'=>['$gt'=>0]]; //查询条件 user_id大于0 $options = [    'projection' => ['_id' => 0], //不输出_id字段    'sort' => ['user_id'=>-1] //根据user_id字段排序 1是升序,-1是降序 ]; $query = new MongoDB\Driver\Query($filter, $options); //查询请求 $list = $manager->executeQuery('location.box',$query); // 执行查询 location数据库下的box集合   foreach ($list as $document) {     print_r($document);  }

查询更多条件使用方法,参考第二节mongodb基本命令,查询

3:添加

<?php  //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017');  $bulk = new MongoDB\Driver\BulkWrite; //默认是有序的,串行执行 //$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase,并行执行 $bulk->insert(['user_id' => 2, 'real_name'=>'中国',]); $bulk->insert(['user_id' => 3, 'real_name'=>'中国人',]); $manager->executeBulkWrite('location.box', $bulk); //执行写入 location数据库下的box集合

4:修改

<?php  //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017');  $bulk = new MongoDB\Driver\BulkWrite; //默认是有序的,串行执行 //$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase,并行执行 $bulk->update( 	['user_id' => 2], 	['$set'=>['real_name'=>'中国国'] ]);  //$set相当于mysql的 set,这里和mysql有两个不同的地方, //1:字段不存在会添加一个字段; //2:mongodb默认如果条件不成立,新增加数据,相当于insert   //如果条件不存在不新增加,可以通过设置upsert //db.collectionName.update(query, obj, upsert, multi);  $bulk->update( 	['user_id' => 5], 	[ 		'$set'=>['fff'=>'中国国'] 	], 	['multi' => true, 'upsert' => false]  	//multi为true,则满足条件的全部修改,默认为true,如果改为false,则只修改满足条件的第一条 	//upsert为 treu:表示不存在就新增 ); $manager->executeBulkWrite('location.box', $bulk); //执行写入 location数据库下的box集合

ordered 设置

5:删除

<?php  //链接mongodb $manager = new MongoDB\Driver\Manager('mongodb://root:sjhc168@10.10.10.104:27017');  $bulk = new MongoDB\Driver\BulkWrite; //默认是有序的,串行执行 //$bulk = new MongoDB\Driver\BulkWrite(['ordered' => flase]);//如果要改成无序操作则加flase,并行执行 $bulk->delete(['user_id'=>5]);//删除user_id为5的字段 $manager->executeBulkWrite('location.box', $bulk); //执行写入 location数据库下的box集合

delete还可以通过limit设置不同删除方式

$bulk->delete(['user_id' => 1], ['limit' => 1]);   // limit 为 1 时,删除第一条匹配数据 $bulk->delete(['user_id' => 2], ['limit' => 0]);   // limit 为 0 时,删除所有匹配数据,默认删除所有

6:捕获异常

MongoDB\Driver\Exception\AuthenticationException MongoDB\Driver\Exception\BulkWriteException MongoDB\Driver\Exception\ConnectionException MongoDB\Driver\Exception\ConnectionTimeoutException MongoDB\Driver\Exception\Exception //所有驱动程序异常的公共接口 MongoDB\Driver\Exception\ExecutionTimeoutException MongoDB\Driver\Exception\InvalidArgumentException MongoDB\Driver\Exception\LogicException MongoDB\Driver\Exception\RuntimeException MongoDB\Driver\Exception\SSLConnectionException MongoDB\Driver\Exception\UnexpectedValueException MongoDB\Driver\Exception\WriteException

说明:http://php.net/manual/zh/class.mongodb-driver-exception-authenticationexception.php

以上就是php7的mongodb基本用法(代码详解)的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:php7 安装指南(windows)之开启zend opcache

相关资讯