IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

三十五、PHP7 MongDB 扩展安装与使用

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=3701
在前面的章节中我们学习了 MongoDB PHP 扩展使用范例,不过那篇文章只能针对 PHP5 使用,PHP7 以上版本则需要使用其它的 PHP MongoDB 扩展

PHP7 Mongdb 扩展安装

假设我们的 PHP7 安装在 /usr/local/php7 目录

我们可以使用 pecl 命令来安装 PHP MongoDB 扩展

$ /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 使用范例

PHP7 连接 MongoDB 数据库

<?php

/*
 * filename: main.php
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/


$driver = new MongoDB\Driver\Manager("mongodb://localhost:27017");

PHP7 MongoDB 插入数据

我们可以使用以下代码将 Python 插入到 souyunku 数据库的 language 集合中

<?php

/*
 * filename: main.php
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/


$driver = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'Python'];

$_id= $bulk->insert($document);

var_dump($_id);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $driver->executeBulkWrite('test.language', $bulk, $writeConcern);

运行以上 PHP7 脚本,输出结果如下

$ php main.php                           
object(MongoDB\BSON\ObjectId)#4 (1) {
  ["oid"]=>
  string(24) "59eef1902a2f950ad3720422"
}

读取数据

我们先将 PerlPHPRuby 插入到 language 集合中,然后读取迭代出来

<?php

/*
 * filename: main.php
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/


$driver = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->insert(["name"=> "Perl"]);
$bulk->insert(["name"=> "PHP"]);
$bulk->insert(["name"=> "Ruby"]);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $driver->executeBulkWrite('test.language', $bulk, $writeConcern);


$filter = [];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['name' => -1],
];

// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $driver->executeQuery('test.language', $query);

foreach ($cursor as $document) {
    print_r($document);
}

运行以上 PHP7 脚本,输出结果如下

$ php main.php
stdClass Object
(
    [name] => Ruby
)
stdClass Object
(
    [name] => Python
)
stdClass Object
(
    [name] => Perl
)
stdClass Object
(
    [name] => PHP
)

更新数据

我们将 PHP 语言改成 PHP7

<?php

/*
 * filename: main.php
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/


$driver = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->update(
    ['name' => 'PHP'],
    ['$set' => ['name' => 'PHP7']],
    ['multi' => false, 'upsert' => false]
);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $driver->executeBulkWrite('test.language', $bulk, $writeConcern);


$filter = [];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['name' => -1],
];

// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $driver->executeQuery('test.language', $query);

foreach ($cursor as $document) {
    print_r($document);
}

运行以上 PHP7 脚本,输出结果如下

$ php main.php
stdClass Object
(
    [name] => Ruby
)
stdClass Object
(
    [name] => Python
)
stdClass Object
(
    [name] => Perl
)
stdClass Object
(
    [name] => PHP7
)

删除数据

现在我们删除 PHP7 这条数据

<?php

/*
 * filename: main.php
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/


$driver = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->delete(['name' => 'PHP7'], ['limit' => 0]);  // limit 为 0 时,删除所有匹配数据

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $driver->executeBulkWrite('test.language', $bulk, $writeConcern);


$filter = [];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['name' => -1],
];

// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $driver->executeQuery('test.language', $query);

foreach ($cursor as $document) {
    print_r($document);
}

运行以上 PHP7 脚本,输出结果如下

$ php main.php
stdClass Object
(
    [name] => Ruby
)
stdClass Object
(
    [name] => Python
)
stdClass Object
(
    [name] => Perl
)

延伸阅读

更多使用方法请参考:http://php.net/manual/en/book.mongodb.php

干货推荐

本站推荐:精选优质专栏

附录:MongoDB 教程:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(94) 打赏



未经允许不得转载:搜云库技术团队 » 三十五、PHP7 MongDB 扩展安装与使用

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367