PHP7 连接使用MongoDB API
老版本MongoDB扩展是不支持最新的PHP7,因此PHP7以上使用MongoDB,需要下载重新编译支持PHP7的扩展
MongoDB官网:https://www.mongodb.com,最新版 mongodb-3.6.0.tgz
Mongo 扩展官网: http://pecl.php.net/package/mongo (支持旧API,不推荐)
MongoDB 扩展官网:http://pecl.php.net/package/mongodb (支持新API,推荐)
PHP7 MongoDB API 官网:http://php.net/manual/en/book.mongodb.php (新API,支持PHP7,推荐)
米扑博客的系统环境
1) lsb_release -a
Description: CentOS Linux release 7.2.1511 (Core)
2) php -v
PHP 7.2.0 (cli) (built: Dec 12 2017 22:38:54) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.2.0, Copyright (c) 1999-2017, by Zend Technologies
3) mongod --version
db version v3.2.8
1. PHP7 简单插入数据到MongoDB
PHP 代码
$host = '127.0.0.1'; $port = 27817; $dbname = 'test'; $conn_conf = sprintf("mongodb://%s:%d/%s", $host, $port, $dbname); $manager = new MongoDB\Driver\Manager($conn_conf); echo "<br><br>manager : <br>"; var_dump($manager); $bulk = new MongoDB\Driver\BulkWrite; $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'mimvp_money']; $_id= $bulk->insert($document); echo "<br><br>id : <br>"; var_dump($_id); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.coll_php7', $bulk, $writeConcern); echo "<br><br>result : <br>"; var_dump($result);
运行结果:
manager :
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(41) "mongodb://120.24.177.37:27517/mimvp_money" ["cluster"]=> array(0) { } }
id :
object(MongoDB\BSON\ObjectId)#4 (1) { ["oid"]=> string(24) "5a30f0fd8f32bd1cc70a6d72" }
result :
object(MongoDB\Driver\WriteResult)#6 (9) { ["nInserted"]=> int(1) ["nMatched"]=> int(0) ["nModified"]=> int(0) ["nRemoved"]=> int(0) ["nUpserted"]=> int(0) ["upsertedIds"]=> array(0) { } ["writeErrors"]=> array(0) { } ["writeConcernError"]=> NULL ["writeConcern"]=> object(MongoDB\Driver\WriteConcern)#7 (2) { ["w"]=> string(8) "majority" ["wtimeout"]=> int(1000) } }
从打印结果看,PHP7 MongoDB API 返回的都是object,需要转化成array才可以使用
查看插入的结果:
> use test; switched to db test > db.coll_php7.findOne() { "_id" : ObjectId("5a30f0fd8f32bd1cc70a6d72"), "name" : "mimvp_money" }
2. PHP7 批量插入数据到MongoDB
PHP 代码
$host = '127.0.0.1'; $port = 27817; $dbname = 'test'; $conn_conf = sprintf("mongodb://%s:%d/%s", $host, $port, $dbname); $manager = new MongoDB\Driver\Manager($conn_conf); $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert(['id' => 81, 'name'=>'money', 'url' => 'https://money.mimvp.com']); $bulk->insert(['id' => 82, 'name'=>'blog', 'url' => 'https://blog.mimvp.com']); $bulk->insert(['id' => 83, 'name'=>'site', 'url' => 'https://site.mimvp.com']); $result = $manager->executeBulkWrite('test.coll_sites', $bulk); echo "<br><br>result : <br>"; var_dump($result);
运行结果:
result :
object(MongoDB\Driver\WriteResult)#6 (9) { ["nInserted"]=> int(3) ["nMatched"]=> int(0) ["nModified"]=> int(0) ["nRemoved"]=> int(0) ["nUpserted"]=> int(0) ["upsertedIds"]=> array(0) { } ["writeErrors"]=> array(0) { } ["writeConcernError"]=> NULL ["writeConcern"]=> object(MongoDB\Driver\WriteConcern)#7 (0) { } }
查看插入的结果:
> db.coll_sites.find() { "_id" : ObjectId("5a30f4d18f32bd203f5872b2"), "id" : 81, "name" : "money", "url" : "https://money.mimvp.com" } { "_id" : ObjectId("5a30f4d18f32bd203f5872b3"), "id" : 82, "name" : "blog", "url" : "https://blog.mimvp.com" } { "_id" : ObjectId("5a30f4d18f32bd203f5872b4"), "id" : 83, "name" : "site", "url" : "https://site.mimvp.com" }
3. PHP7 从MongoDB查询数据
PHP 代码
$host = '127.0.0.1'; $port = 27817; $dbname = 'test'; $conn_conf = sprintf("mongodb://%s:%d/%s", $host, $port, $dbname); $manager = new MongoDB\Driver\Manager($conn_conf); // 批量插入数据 $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert(['id' => 81, 'name'=>'money', 'url' => 'https://money.mimvp.com']); $bulk->insert(['id' => 82, 'name'=>'blog', 'url' => 'https://blog.mimvp.com']); $bulk->insert(['id' => 83, 'name'=>'site', 'url' => 'https://site.mimvp.com']); $result = $manager->executeBulkWrite('test.coll_sites', $bulk); // 过滤查询数据 $filter = ['id' => ['$gte' => 82]]; $options = ['projection' => ['_id' => 0], 'sort' => ['id' => -1], ]; $query = new MongoDB\Driver\Query($filter, $options); $cursor = $manager->executeQuery('test.coll_sites', $query); // 输出object // foreach ($cursor as $document) { // print_r($document); // } // object转array (推荐) foreach ($cursor as $document) { $doc_array = (array)$document; $id = $doc_array['id']; $name = $doc_array['name']; $url = $doc_array['url']; $doc_str = sprintf("%d \t %s \t %s", $id, $name, $url); echo "<br> $doc_str"; }
运行结果:
这里的运行结果格式,有两种方式
方式1:输出 object
// 输出object
foreach ($cursor as $document) {
print_r($document);
}
查询结果如下:
stdClass Object ( [id] => 83 [name] => site [url] => https://site.mimvp.com ) stdClass Object ( [id] => 82 [name] => blog [url] => https://blog.mimvp.com )
方式2:object转array (推荐)
// object转array (推荐) foreach ($cursor as $document) { $doc_array = (array)$document; $id = $doc_array['id']; $name = $doc_array['name']; $url = $doc_array['url']; $doc_str = sprintf("%d \t %s \t %s", $id, $name, $url); echo "<br> $doc_str"; }
查询结果如下:
83 site https://site.mimvp.com
82 blog https://blog.mimvp.com
4. PHP7 更新数据到MongoDB
PHP 代码
$host = '127.0.0.1'; $port = 27817; $dbname = 'test'; $conn_conf = sprintf("mongodb://%s:%d/%s", $host, $port, $dbname); $manager = new MongoDB\Driver\Manager($conn_conf); $bulk = new MongoDB\Driver\BulkWrite; $bulk->update( ['id' => 81], ['$set' => ['name' => 'proxy', 'url' => 'https://proxy.mimvp.com']], ['multi' => false, 'upsert' => false] ); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.coll_sites', $bulk, $writeConcern); echo "<br><br>result: <br>"; var_dump($result);
运行结果:
result:
object(MongoDB\Driver\WriteResult)#4 (9) { ["nInserted"]=> int(0) ["nMatched"]=> int(1) ["nModified"]=> int(1) ["nRemoved"]=> int(0) ["nUpserted"]=> int(0) ["upsertedIds"]=> array(0) { } ["writeErrors"]=> array(0) { } ["writeConcernError"]=> NULL ["writeConcern"]=> object(MongoDB\Driver\WriteConcern)#5 (2) { ["w"]=> string(8) "majority" ["wtimeout"]=> int(1000) } }
查询结果如下:
> db.coll_sites.find() { "_id" : ObjectId("5a30f6ee8f32bd261342b942"), "id" : 81, "name" : "proxy", "url" : "https://proxy.mimvp.com" } { "_id" : ObjectId("5a30f6ee8f32bd261342b943"), "id" : 82, "name" : "blog", "url" : "https://blog.mimvp.com" } { "_id" : ObjectId("5a30f6ee8f32bd261342b944"), "id" : 83, "name" : "site", "url" : "https://site.mimvp.com" }
5. PHP7 删除 MongoDB 里的数据
PHP 代码
$host = '127.0.0.1'; $port = 27817; $dbname = 'test'; $conn_conf = sprintf("mongodb://%s:%d/%s", $host, $port, $dbname); $manager = new MongoDB\Driver\Manager($conn_conf); $bulk = new MongoDB\Driver\BulkWrite; $bulk->delete(['id' => 81], ['limit' => 1]); // limit=1,删除第一条匹配数据 $bulk->delete(['id' => 82], ['limit' => 0]); // limit=0,删除所有匹配数据 $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.coll_sites', $bulk, $writeConcern); echo "<br><br>result: <br>"; var_dump($result);
运行结果:
result:
object(MongoDB\Driver\WriteResult)#4 (9) { ["nInserted"]=> int(0) ["nMatched"]=> int(0) ["nModified"]=> int(0) ["nRemoved"]=> int(2) ["nUpserted"]=> int(0) ["upsertedIds"]=> array(0) { } ["writeErrors"]=> array(0) { } ["writeConcernError"]=> NULL ["writeConcern"]=> object(MongoDB\Driver\WriteConcern)#5 (2) { ["w"]=> string(8) "majority" ["wtimeout"]=> int(1000) } }
查询结果如下:
> db.coll_sites.find() { "_id" : ObjectId("5a30f6ee8f32bd261342b944"), "id" : 83, "name" : "site", "url" : "https://site.mimvp.com" }
参考推荐:
PHP 把对象 stdClass Object 转数组 array
Linux php7安装mongoDB和memcached扩展
LNMP(CentOS+Nginx+Mysql+PHP)服务器环境配置
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2018-06-05 05:46:59
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!