PHP7环境中MongDB怎样安装,增删改操作怎样做
Admin 2022-09-08 群英技术资讯 947 次浏览
今天就跟大家聊聊有关“PHP7环境中MongDB怎样安装,增删改操作怎样做”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“PHP7环境中MongDB怎样安装,增删改操作怎样做”文章能对大家有帮助。本文教程只适合在 PHP7 的环境,如果你是 PHP5 环境,你可以参阅 PHP 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 命令的路径。
PHP7 连接 MongoDB 语法如下:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
将 name 为"菜鸟教程" 的数据插入到 test 数据库的 runoob 集合中。
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => '菜鸟教程'];
$_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.runoob', $bulk, $writeConcern);
?>
这里我们将三个网址数据插入到 test 数据库的 sites 集合,并读取迭代出来:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'菜鸟教程', 'url' => 'http://www.runoob.com']);
$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' => '菜鸟工具', 'url' => 'tool.runoob.com']],
['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 的数据已经变成了菜鸟工具:

以下实例删除了 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);
?>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
数据类型是数据库学习需要掌握的知识,对此这篇文章给大家分享有关MongoDB数据类型的内容,小编觉得挺实用的,需要的朋友可以参考学习,下面就跟随小编看看吧。
Linux平台安装MongoDB MongoDB 提供了 linux 各个发行版本 64 位的安装包,你可以在官网下载安装包。 安装前我们需要安装各个 Linux 平台依赖包。 Red Hat/CentOS: sudo yum install libcurl openssl Ubuntu 18.04 LTS (“Bionic”)/Debian 10 “Buster”: sudo apt-get install libcurl4 o..
在MongoDB中时间存储方式:1、将时间戳存为Number格式。2、使用MongoDB自带的日期格式存储。
Windows 平台安装 MongoDB MongoDB 下载 MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mongodb.com/download-center/community 注意:在 MongoDB 2.2 版本后已经不再支持 Windows XP 系统。最新版本也已经没有了 32 位系统的安装..
MongoDB PHP 在php中使用mongodb你必须使用 mongodb 的 php驱动。 MongoDB PHP在各平台上的安装及驱动包下载请查看:PHP安装MongoDB扩展驱动 如果你使用的是 PHP7,请参阅:PHP7 MongoDB 安装与使用。 确保连接及选择一个数据库 为了确保正确连接,你需要指定数据库名,如果数据库在mongoDB中不存在,mongoDB会自动创建 代码片段如下: 创建集合 创建集合的..
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008