MongoDB入门---安装php扩展&php基本增删改查操作&php7基本数据操作
经过前些天的学习,我们对MongoDB有了一个大概的了解了,对于命令行的操作,也有了基本的认识。但是呢,无论什么数据库,最终还是要落实到操作语言上。因为本人擅长的是php这个语言,所以本人就。。。谁让php是世界上最好的语言呢。。。憋打我哈,接下来直接进入正题哈。。。
首先是安装php扩展。你可以在linux中执行以下命令来安装MongoDB 的 PHP 扩展驱动:
$ sudo pecl install mongo //使用php的pecl安装命令必须保证网络连接可用以及root权限
那么,接下来就是通过源码来编译扩展驱动。但是必须手动编译源码包,这样做的好是最新修正的bug包含在源码包中。我们可以在Github上下载MongoDB PHP驱动包。访问github网站然后搜索"mongo php driver"(下载地址:https://github.com/mongodb/mongo-php-driver),下载该源码包,然后执行以下命令:
$ git clone https://github.com/mongodb/mongo-php-driver.git $ cd mongo-php-driver $ git submodule sync && git submodule update --init $ phpize $ ./configure $ make all -j 5 $ sudo make install
如果你的php是自己编译的,则安装方法如下(假设是编译在/usr/local/php目录中):
$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule sync && git submodule update --init
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make all -j 5
$ sudo make install
执行完事上面的例子之后,我们需要修改php.ini文件,在php.ini文件中添加mongo配置,配置如下:
extension=mongo.so
注意:你需要指明 extension_dir 配置项的路径。
大家还可以在windows上安装扩展。PECL 上已经提供了用于 Window 平台的预编译 php mongodb 驱动二进制包(下载地址: https://pecl.php.net/package/mongodb),你可以下载与你php对应的版本,但是你需要注意以下几点问题:
- VC6 是运行于 Apache 服务器
- 'Thread safe'(线程安全)是运行在Apache上以模块的PHP上,如果你以CGI的模式运行PHP,请选择非线程安全模式(' non-thread safe')。
- VC9是运行于 IIS 服务器上。
- 下载完你需要的二进制包后,解压压缩包,将'php_mongo.dll'文件添加到你的PHP扩展目录中(ext)。ext目录通常在PHP安装目录下的ext目录。
完事之后,我们需要打开php配置文件 php.ini 添加以下配置:
extension=php_mongo.dll
接着呢,我们来重启服务器,通过浏览器访问phpinfo,如果安装成功,就会看到类型以下的信息:
接下来就是MAC中安装MongoDB PHP扩展驱动了,你可以使用'autoconf'安装MongoDB PHP扩展驱动。还可以使用'Xcode'安装MongoDB PHP扩展驱动。如果你使用 XAMPP,你可以使用以下命令安装MongoDB PHP扩展驱动:
sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongo
如果以上命令在XMPP或者MAMP中不起作用,你需要在Github上下载兼容的预编译包。然后添加 'extension=mongo.so' 配置到你的php.ini文件中。
好啦,到这里呢,关于MongoDB PHP的扩展驱动安装方式就差不多结束了,声明一下哈,只有那个什么linux的安装方式是我自己试验过的,其他的没有哈,各位如果安装失败的话,不如问问度娘哈。。。废话不多说啦,直接看PHP是如何来使用MongoDB这个数据库的。
首先,为了确保正确连接,你需要指定数据库名,如果数据库在mongoDB中不存在,mongoDB会自动创建。如下:
<?php $m = new MongoClient(); // 连接默认主机和端口为:mongodb://localhost:27017 $db = $m->test; // 获取名称为 "test" 的数据库 ?>
先来创建集合哈:
<?php $m = new MongoClient(); // 连接 $db = $m->test; // 获取名称为 "test" 的数据库 $collection = $db->createCollection("luyaran"); echo "集合创建成功"; ?>
执行以上程序,输出结果如下:
集合创建成功
接下来,我们在mongoDB中使用 insert() 方法插入文档:
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->luyaran; // 选择集合 $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.luyaran.com/mongodb/", "by", "luyaran" ); $collection->insert($document); echo "数据插入成功"; ?>
执行以上程序,输出结果如下:
数据插入成功
然后我们再数据库命令行的状态下使用db.runoob.find().pretty()查看数据库中luyaran这个集合中的数据就好了。如果对自己的代码有信心的话,可以接着往下看。就接着使用find() 方法来读取集合中的文档。
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->luyaran; // 选择集合 $cursor = $collection->find(); // 迭代显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
执行以上程序,输出结果如下:
MongoDB
好啦,接下来就看一下修改,我们使用 update() 方法来更新文档:
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->luyaran; // 选择集合 // 更新文档 $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程"))); // 显示更新后的文档 $cursor = $collection->find(); // 循环显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
执行以上程序,输出结果如下:
MongoDB 教程
完整的结果集,大家可以通过之前的查询方式查看哈。好啦,我们再来使用 remove() 方法来删除文档,以下实例中我们将移除 'title' 为 'MongoDB 教程' 的一条数据记录:
<?php $m = new MongoClient(); // 连接到mongodb $db = $m->test; // 选择一个数据库 $collection = $db->luyaran; // 选择集合 // 移除文档 $collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true)); // 显示可用文档数据 $cursor = $collection->find(); foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>除了以上实例外,在php中你还可以使用findOne(), save(), limit(), skip(), sort()等方法来操作Mongodb数据库,
更多的操作方法可以参考 Mongodb 核心类:http://php.net/manual/zh/mongo.core.php。
到这里呢,基本的数据操作,就分享的差不多了。不过近来PHP7这个鬼这么火,如果不研究一下,那就太可惜了。好啦,不开玩笑了,直接来看看哈。首先使用 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 为"luyaran" 的数据插入到 test 数据库的 luyaran 集合中:
<?php $bulk = new MongoDB\Driver\BulkWrite; $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'luyaran']; $_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.luyaran', $bulk, $writeConcern); ?>//执行结果自行按照之前的方式查看哈
再来呢,将三个网址数据插入到 test 数据库的 sites 集合,并读取迭代出来:
<?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); // 插入数据 $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert(['x' => 1, 'name'=>'luyaran', 'url' => 'http://www.luyaran.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' => 'hanyanxiao', 'url' => 'tool.hanyanxiao.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 为 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); ?>具体产生的数据变化,大家可以在后台慢慢查看,本人就不做赘述了,今天的分享到这里就差不多完事了,各位勿喷哈,完事了呢,如果觉得还不错的话,请多多点赞支持哈。。。