前言
本文主要给大家介绍了关于php利用pecl安装mongodb扩展的相关内容,下面话不多说了,来一起看看详细的介绍吧
环境说明
- php7
- centos7
- mongodb4.0.5
默认情况下,php并没有安装mongodb扩展,会报Class "MongoDBDriverQuery" not found
错误。
pecl安装扩展
通过pecl可以很方便地安装扩展
注意:如果安装了多个版本的php,需要进行相应版本的pecl目录,如:
➜ bin pwd/usr/local/php7.1/bin➜ bin sudo ./pecl install mongodb
安装完成后,在php.ini添加:extension=mongodb.so
重启php, 查看 phpinfo
测试
<?php $manager = new MongoDBDriverManager("mongodb://localhost:27017");// 插入数据$bulk = new MongoDBDriverBulkWrite;$bulk->insert(["id" => 1, "name"=>"Google"]);$bulk->insert(["id" => 2, "name"=>"Github"]);$bulk->insert(["id" => 3, "name"=>"StackOverFlow"]);$manager->executeBulkWrite("test.sites", $bulk);$filter = ["id" => ["$gt" => 1]];$options = [ "projection" => ["_id" => 0], "sort" => ["id" => -1],];// 查询数据$query = new MongoDBDriverQuery($filter, $options);$cursor = $manager->executeQuery("test.sites", $query);foreach ($cursor as $document) { echo "<pre>"; var_dump($document);}
执行结果:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对网页设计的支持。