你可以在 PHP 官网上下载 MongoDB PHP 驱动包,下载地址:http://pecl.php.net/package/mongodb。
[root@kafka2 ~]# wget https://pecl.php.net/get/mongodb-1.13.0.tgz [root@kafka2 ~]# tar -zxvf mongodb-1.13.0.tgz [root@kafka2 ~]# mv mongodb-1.13.0 /usr/local/src/ [root@kafka2 ~]# cd /usr/local/src/ [root@kafka2 src]# cd mongodb-1.13.0/ [root@kafka2 mongodb-1.13.0]# find / -name phpize /usr/local/src/php/scripts/phpize /usr/local/php/bin/phpize [root@kafka2 mongodb-1.13.0]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20180731 Zend Module Api No: 20180731 Zend Extension Api No: 320180731 [root@kafka2 mongodb-1.13.0]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@kafka2 mongodb-1.13.0]# make && make install ... Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/ [root@kafka2 mongodb-1.13.0]# vim /usr/local/php/etc/php.ini extension=mongodb [root@kafka2 mongodb-1.13.0]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@kafka2 mongodb-1.13.0]# systemctl restart php-fpm.service [root@kafka2 mongodb-1.13.0]# systemctl restart nginx.service
新建 mongodb.php
插入数据:将 name 为"菜鸟教程" 的数据插入到 test 数据库的 runoob 集合中。
<?php $bulk = new MongoDB\Driver\BulkWrite; $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => '菜鸟教程']; $_id= $bulk->insert($document); var_dump($_id); $manager = new MongoDB\Driver\Manager("mongodb://192.168.142.129:27017"); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.runoob', $bulk, $writeConcern); var_dump($result);
读取数据
<?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); // 插入数据 $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert(['x' => 1, 'name'=>'菜鸟教程', 'url' => 'http://www.runoob.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 )
更新数据
<?php $bulk = new MongoDB\Driver\BulkWrite; $bulk->update( ['x' => 2], ['$set' => ['name' => '菜鸟工具', 'url' => 'tool.runoob.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 为 2 的数据已经变成了菜鸟工具:
删除数据
以下实例删除了 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);
PHP操作Mongodb封装类:
https://docs.mongodb.com/php-library/current/tutorial/
https://docs.mongodb.com/ecosystem/drivers/php/
参考:
https://github.com/mongodb/mongo-php-library
https://blog.csdn.net/weixin_36083698/article/details/112019246
https://blog.csdn.net/color_wind/article/details/52008674
http://php.net/manual/en/book.mongodb.php
https://www.php.net/manual/zh/mongodb.tutorial.library.php
https://blog.csdn.net/naidad/article/details/105936872
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn