当前位置:网站首页>关于 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;
}
学习交流
边栏推荐
- Pay attention to the details of pytoch code, and it is easy to make mistakes
- 养了只小猫咪
- Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
- Pytorch代码注意的细节,容易敲错的地方
- SequoiaDB湖仓一体分布式数据库2022.6月刊
- Embedded interview questions (I: process and thread)
- Vulhub vulnerability recurrence 73_ Webmin
- P2802 go home
- Migrate Infones to stm32
- 27io stream, byte output stream, OutputStream writes data to file
猜你喜欢
Construction of yolox based on paste framework
Clear floating mode
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
Station B, Master Liu Er - dataset and data loading
【SQL server速成之路】——身份验证及建立和管理用户账户
26file filter anonymous inner class and lambda optimization
Game push image / table /cv/nlp, multi-threaded start
28io stream, byte output stream writes multiple bytes
局域网同一个网段通信过程
Station B, Master Liu Er - back propagation
随机推荐
Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
[email protected] raspberry pie
初识数据库
华为BFD的配置规范
改善Jpopup以实现动态控制disable
Rustdesk builds its own remote desktop relay server
How to download GB files from Google cloud hard disk
Web Security (V) what is a session? Why do I need a session?
[detailed explanation of Huawei machine test] statistics of shooting competition results
进程和线程
How to use PHP string query function
High quality coding tool clion
[SQL Server fast track] - authentication and establishment and management of user accounts
B站刘二大人-Softmx分类器及MNIST实现-Lecture 9
Jvxetable用slot植入j-popup
Detailed summary of SQL injection
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
Web Security (VI) the use of session and the difference between session and cookie
Jushan database appears again in the gold fair to jointly build a new era of digital economy
How can large websites choose better virtual machine service providers?