MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongDB PHP7


PHP7 MongDB Installation and Usage

This tutorial is only suitable for PHP7 environment. If you are in PHP5 environment, you can refer to PHP MongDB Installation and Usage.

PHP7 Mongdb extension installation

We use the pecl command to install:

$ /usr/local/php7/bin/pecl install mongodb

After successful execution, the following results will be output:

……
Build process completed successfully
Installing '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.1.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini

Next we open php.ini file, add extension=mongodb.so configuration.

You can directly execute the following command to add.

$ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

Note: The installation directory of php7 in the above executed command is /usr/local/php7/. If you install it in other directories, you need to modify the pecl and php commands accordingly. path of.


Mongodb Use

PHP7 to connect to MongoDB. The syntax is as follows:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

Insert data

Set the name to "php Chinese website" The data is inserted into the php collection of the test database.

<?php
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'php中文网'];

$_id= $bulk->insert($document);

var_dump($_id);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.php', $bulk, $writeConcern);
?>

Reading data

Here we insert the three URL data into the sites collection of the test database, and read and iterate it out:

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  

// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'php中文网', 'url' => 'http://www.php.cn']);
$bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk);

$filter = ['x' => ['$gt' => 1]];
$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);
}
?>

The output result is:

stdClass Object
(
    [x] => 3
    [name] => taobao
    [url] => http://www.taobao.com
)
stdClass Object
(
    [x] => 2
    [name] => Google
    [url] => http://www.google.com
)

Update data

Next we will update the data where x is 2 in the test database sites collection:

<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
    ['x' => 2],
    ['$set' => ['name' => 'php工具', 'url' => 'tool.php.cn']],
    ['multi' => false, 'upsert' => false]
);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>

Next we use the "db.sites.find()" command Check the changes in the data. The data with x as 2 has been turned into a php tool:

Delete data

The following example deletes x as 1 and x as 2 data, pay attention to the difference in limit parameters:

<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['x' => 1], ['limit' => 1]);   // limit 为 1 时,删除第一条匹配数据
$bulk->delete(['x' => 2], ['limit' => 0]);   // limit 为 0 时,删除所有匹配数据

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
?>

For more usage methods, please refer to: http://php.net/manual/en/book.mongodb.php.

php.cn