当前位置:网站首页>关于 PHP 启动 MongoDb 找不到指定模块问题
关于 PHP 启动 MongoDb 找不到指定模块问题
2022-07-06 05:47:00 【北桥苏】
前言:
最近有一个小 demo,需要通过 PHP 将用户行为记录储存到 MongoDB,再用 Spark 做协同过滤。由于以前处理跨语言交互是通过消息中间件,这次本地使用 MongoDB 却弄出了几个问题。首先是本地安装了扩展,启动时报找不到模块错误,再个时 PHP 进行的实例化使用时报 MongoClient 和一些函数不存在。最后也推荐一些方便快捷的 MongoDB 图形化工具。
环境:
1. Windows10
2. PHP7.4
3. MongoDB 4.4.14 Server
5. php_mongodb-1.12.1-7.4-nts-vc15-x64
安装:

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;
}学习交流

边栏推荐
- 清除浮动的方式
- Station B, Mr. Liu Er - multiple logistic regression, structure 7
- 03. Login of development blog project
- Jvxetable implant j-popup with slot
- Zoom through the mouse wheel
- SequoiaDB湖仓一体分布式数据库2022.6月刊
- LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
- Pytorch代码注意的细节,容易敲错的地方
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- [imgui] unity MenuItem shortcut key
猜你喜欢

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Deep learning -yolov5 introduction to actual combat click data set training

Installation de la Bibliothèque de processus PDK - csmc

First knowledge database
![[experience] install Visio on win11](/img/f5/42bd597340d0aed9bfd13620bb0885.png)
[experience] install Visio on win11

PDK process library installation -csmc

Yunxiaoduo software internal test distribution test platform description document
【SQL server速成之路】——身份验证及建立和管理用户账户

Analysis of grammar elements in turtle Library

JS array list actual use summary
随机推荐
Implementation of linked list in address book management system
移植InfoNES到STM32
查询生产订单中某个(些)工作中心对应的标准文本码
Yygh-11-timing statistics
Detailed summary of SQL injection
Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
Summary of deep learning tuning tricks
Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
Station B, Mr. Liu Er - multiple logistic regression, structure 7
59. Spiral matrix
29io stream, byte output stream continue write line feed
Vulhub vulnerability recurrence 73_ Webmin
c语言——冒泡排序
自建DNS服务器,客户端打开网页慢,解决办法
Redis message queue
Web Security (VI) the use of session and the difference between session and cookie
Vulhub vulnerability recurrence 72_ uWSGI
Station B Liu Erden - linear regression and gradient descent
Jushan database appears again in the gold fair to jointly build a new era of digital economy
【SQL server速成之路】——身份验证及建立和管理用户账户