当前位置:网站首页>关于 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;
}学习交流

边栏推荐
- 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
- Redis message queue
- P2802 回家
- Auto.js学习笔记17:基础监听事件和UI简单的点击事件操作
- [force buckle]43 String multiplication
- Query the standard text code corresponding to a work center (s) in the production order
- Redistemplate common collection instructions opsforvalue (II)
- Sword finger offer II 039 Maximum rectangular area of histogram
- ArcGIS应用基础4 专题图的制作
- B站刘二大人-线性回归 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

P2802 回家

Promise summary

网站进行服务器迁移前应做好哪些准备?

Analysis of grammar elements in turtle Library

Construction of yolox based on paste framework

Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist

实践分享:如何安全快速地从 Centos迁移到openEuler

How can large websites choose better virtual machine service providers?

Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
随机推荐
Clear floating mode
H3C S5820V2_5830V2交换机IRF2堆叠后升级方法
Summary of deep learning tuning tricks
01. Project introduction of blog development project
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
Garbage collector with serial, throughput priority and response time priority
实践分享:如何安全快速地从 Centos迁移到openEuler
嵌入式面试题(四、常见算法)
ArcGIS application foundation 4 thematic map making
Improve jpopup to realize dynamic control disable
Quantitative description of ANC noise reduction
Self built DNS server, the client opens the web page slowly, the solution
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
通讯录管理系统链表实现
移植InfoNES到STM32
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
[Jiudu OJ 07] folding basket
Jvxetable implant j-popup with slot
After the project is released, index Html is cached