2. PHP7.4

3. MongoDB 4.4.14 Server

5. php_mongodb-1.12.1-7.4-nts-vc15-x64

php找不到dll关于 PHP 启动 MongoDb 找不到指定模块问题 PHP

安装:

1. MongoDB Server 下载

2. php_mongodb-1.12.1-7.4-nts-vc15-x64 下载

3. navicat for mongodb 下载

问题:

1. 找不到指定模块 php_mongodb

PHP 中添加扩展 php_mongodb 禁绝确,除了须要 php_mongodb.dll 还要 php_mongodb.pdb 复制到 php 安装目录下的 ext 中

末了在 php.ini 添加 extension=php_mongodb.dll,重启完后查看 php -m 或者浏览器中查看 phpinfo ()

2. MongoClient 类不存在

缘故原由是这个是旧版本 php_mongo 扩展供应的类,新版本是通过后面演示代码的办法,如果不知道扩展供应的新方法或函数可以通过一下办法查看。

(1). get_declared_classes () 查看扩展里预设的函数

工具利用:

1. MongoDB Server

(1). 启动做事

2. Navicat for MongoDB

(1). 连接做事

代码演示:

1. 查询

public function testMongoDbQuery() { $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017"); $filter = ['x' => ['$gt' => 0]]; $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); } }

2. 插入

public function testMongoDbInsert() { $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017"); $bulk = new \MongoDB\Driver\BulkWrite; // mongoDb默认存在一个自增长的字符串ID $document = [ '_id' => new \MongoDB\BSON\ObjectID, 'videoId' => mt_rand(1, 99999), 'userId' => mt_rand(1, 9999), 'score' => floatval(mt_rand(5,100)), 'date' => time() ]; $bulk->insert($document); $res = $manager->executeBulkWrite('tanhua.recommend_video_20191001', $bulk); return $res; }

3. 批量插入

public function testMongoDbBulk() { //return get_declared_classes(); $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017"); $bulk = new \MongoDB\Driver\BulkWrite; $bulk->insert(['x' => 1, 'name'=>'tenent', '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']); $res = $manager->executeBulkWrite('test.sites', $bulk); return $res; }