php7 mongodb教程,PHP7 MongDB 扩展安装与使用

在前面的章节中我们学习了 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 数据库

/*

* filename: main.php

* author: 简单教程(www.twle.cn)

* Copyright © 2015-2065 www.twle.cn. All rights reserved.

*/

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

PHP7 MongoDB 插入数据

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

/*

* filename: main.php

* author: 简单教程(www.twle.cn)

* Copyright © 2015-2065 www.twle.cn. 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"

}

读取数据

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

/*

* filename: main.php

* author: 简单教程(www.twle.cn)

* Copyright © 2015-2065 www.twle.cn. 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

/*

* filename: main.php

* author: 简单教程(www.twle.cn)

* Copyright © 2015-2065 www.twle.cn. 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 这条数据

/*

* filename: main.php

* author: 简单教程(www.twle.cn)

* Copyright © 2015-2065 www.twle.cn. 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

)

延伸阅读

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值