MongDB PHP7
---恢复内容开始---
PHP7 Mongdb 扩展安装
我们使用 pecl 命令来安装:
$ /usr/local/php7/bin/pecl install mongodb
执行成功后,会输出以下结果:
……
Build process completed successfully
Installing '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.1.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini
接下来我们打开 php.ini 文件,添加 extension=mongodb.so 配置。
可以直接执行以下命令来添加。
$ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
注意:以上执行的命令中 php7 的安装目录为 /usr/local/php7/,如果你安装在其他目录,需要相应修改 pecl 与 php 命令的路径。
Mongodb 使用
PHP7 连接 MongoDB 语法如下:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
插入数据
将 name 为"W3Cschool教程" 的数据插入到 test 数据库的 w3cschool 集合中。
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'W3Cschool教程']; $_id= $bulk->insert($document); var_dump($_id); $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.w3cschool', $bulk, $writeConcern);
?>
读取数据
这里我们将三个网址数据插入到 test 数据库的 sites 集合,并读取迭代出来:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); // 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'W3Cschool教程', 'url' => 'http://www.w3cschool.cn']);
$bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk); $filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1],
]; // 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.sites', $query); foreach ($cursor as $document) {
print_r($document);
}
?>
输出结果为:
stdClass Object
(
[x] => 3
[name] => taobao
[url] => http://www.taobao.com
)
stdClass Object
(
[x] => 2
[name] => Google
[url] => http://www.google.com
)
更新数据
接下来我们将更新 test 数据库 sites 集合中 x 为 2 的数据:
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['x' => 2],
['$set' => ['name' => 'w3cschool在线工具', 'url' => '123.w3cschool.cn/webtools']],
['multi' => false, 'upsert' => false]
); $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>
接下来我们使用 "db.sites.find()" 命令查看数据的变化,x 为 2 的数据已经变成了w3cschool在线工具:
删除数据
以下实例删除了 x 为 1 和 x 为 2的数据,注意 limit 参数的区别:
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['x' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据
$bulk->delete(['x' => 2], ['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('test.sites', $bulk, $writeConcern);
?>
---恢复内容结束---
MongDB PHP7的更多相关文章
- PHP7 MongDB 安装与使用
我们使用 pecl 命令来安装: /usr/local/php7/bin/pecl install mongodb 执行成功后,会输出以下结果: …… Build process completed ...
- TODO:macOS编译PHP7.1
TODO:macOS编译PHP7.1 本文主要介绍在macOS上编译PHP7.1,有兴趣的朋友可以去尝试一下. 1.下载PHP7.1源码,建议到PHP官网下载纯净到源码包php-7.1.0.tar.g ...
- 搭建LNAMP环境(七)- PHP7源码安装Memcached和Memcache拓展
上一篇:搭建LNAMP环境(六)- PHP7源码安装MongoDB和MongoDB拓展 一.安装Memcached 1.yum安装libevent事件触发管理器 yum -y install libe ...
- Centos7安装PHP7
安装依赖 yum updateyum install gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel lib ...
- Ubuntu 16.04 LAMP server 指南 - 配置 Apache2.4,PHP7,和MariaDB(而不是MySQL)
翻译自:https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ 昨天在虚 ...
- CentOS 7.1编译安装PHP7
原文: https://typecodes.com/web/centos7compilephp7.html?utm_source=tuicool&utm_medium=referral 1 创 ...
- 编译安装PHP7并安装Redis扩展Swoole扩展
编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机器上已经有编译安装过php5.3以上的版本,从而依赖库都有了 本php7是编译成fpm-php 使用的, 如果是apache那么 ...
- php7 安装redis扩展
从pecl下载的phpredis扩展在make时报错,google了一下,解决办法,因为phpredis 有专门为php7的分支,所以先git clone phpredis下来 git clone h ...
- 解决升级PHP7后 微信公众号收不到消息
服务器配置Linux+Nginx+PHP5.5+mysql index方法配置微信的关注回复.菜单事件.多客服.自动回复等 public function actionIndex() { if (is ...
随机推荐
- python 类的进阶
类的进阶 一 isinstance(obj,cls)和issubclass(sub,super) class Foo: def __init__(self,name): self.name = nam ...
- 竞赛基础篇---部分和问题(DFS)
问题链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1058 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使 ...
- Spring之事务管理
事务管理对于企业应用至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性. 就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到 ...
- 一次完败的Release
一次完败的Release 去年8月份加入一家创业公司,和原同事做VR相关的产品开发,到18年正月初七,总共release过两次,真正经理了一次从0到1的过程.第一次release产品初步成型,大概在1 ...
- 学习React系列(四)——受控组件与非受控组件
受控组件:通过组件的状态与属性的改变来控制组件 不可控组件:直接通过底层的dom来控制组件(具体来说就是通过绑定再底层dom上的方法来实现的,比如说ref,onChange) 受控组件 functio ...
- 使用poi根据模版生成word文档,支持插入数据和图片
一.制作word模版,${xxxx}是一会要替换的内容,最下面的表格是要插入数据,根据是否以$开头来判断是需要替换还是插入数据, 注意如果是需要插入数据,制作的表格模版需要一行空行,也只能有一行空行, ...
- APP的宣传方式有哪些
APP应用已经成为了互联网不可缺少的话题,事实上,开发一款移动APP的成本不是很高,但是怎样以最低的成本得到最大的推广效果,这是企业和开发者都很关心的一个问题.下面,我们来探讨一下这个问题. 1.一款 ...
- hermite矩阵
在读线代书.因为之前并没有上过线性代数的课.所以决定把基础打牢牢. 读书的时候当然会出现不懂的概念和术语或者定理什么的.所以在这记录一下啦--- hermit矩阵要理解它好像先要知道什么是共轭(con ...
- Python模块 - configparser
configparser模块用来对配置文件进行操作 1.获取所有块 import configparser config = configparser.ConfigParser() config.re ...
- IO流大总结
- - - - - - - - - - - - - - - 写在前面 - - - - - - - - - - - - - - - 1.概念 IO流用来处理设备之间的数据传输 Java对数据的操作是通过 ...