linux系统 php7.2 安装mongodb 1.6扩展

注意: php不同的版本对应的mongodb版本是不同的,用法也完全不同

php mogodb扩展下载地址:https://pecl.php.net/package/mongodb

mongodb说明:https://docs.mongodb.com/php-library/current/tutorial/crud/

安装1.6

下载wget https://pecl.php.net/get/mongodb-1.6.0.tgz

解压tar zxf mongodb-1.6.0.tgz

进入 cd mongodb-1.6.0/

phpize

./configure --with-php-config=php-config

顺利的话会直接提示

Build complete.
Don't forget to run 'make test'.
 
Installing shared extensions:     /usr/lib64/php/modules/
至此,生成so文件成功

下一步打开php扩展就ok了

主要就是在配置文件中加一行代码

extension=mongodb.so
 

 

安装mongodb驱动:

composer require mongodb/mongodb

 

对于指定的链接可以这样写:

$collection = (new MongoDB\Client("mongodb://username:password@ip:port"))->database->table;

插入方法:

<?php
 
$collection = (new MongoDB\Client)->test->users;
 
$insertOneResult = $collection->insertOne([
    'username' => 'admin',
    'email' => 'admin@example.com',
    'name' => 'Admin User',
]);
 
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
 
var_dump($insertOneResult->getInsertedId())

查找方法:

<?php
 
$collection = (new MongoDB\Client)->test->zips;
 
$document = $collection->findOne(['_id' => '94301']);
 
var_dump($document);
limit,skip相当于limit方法:

$collection = (new MongoDB\Client)->test->restaurants;
 
$cursor = $collection->find(
    [
        'cuisine' => 'Italian',
        'borough' => 'Manhattan',
    ],
    [
        'projection' => [
            'name' => 1,
            'borough' => 1,
            'cuisine' => 1,
        ],
        'limit' => 4,
    ]
);
 
foreach($cursor as $restaurant) {
   var_dump($restaurant);

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴国进

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值