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

资源魔 36 0

提醒: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 设置

1:默许是ture,依照程序执行拔出更新数据,假如犯错,中止执行前面的,mongo民间叫串行。
2:假如是false,mongo并发的形式拔出更新数据,两头呈现谬误,没有影响后续操作无影响,mongo民间叫并行

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根本用法(代码详解)的具体内容,更多请存眷资源魔其它相干文章!

标签: PHP7 php7开发教程 php7开发资料 php7开发自学 mongodb基本用法

抱歉,评论功能暂时关闭!