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' => '[email protected]',
    '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);

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章