PHP7使用MongoDB\Driver\Manager类实现增删改查

NoSQL笔记 2019年01月30日

这篇笔记记录了PHP7使用官方扩展MongoDB\Driver提供的Manager/BulkWrite/Query/Cursor/WriteResult类实现增删改查的过程`

相关笔记:
CentOS6.9安装mongodb和php-mongodb扩展
CentOS6.9yum安装mongodb和php-mongodb扩展

1.类说明

作用
MongoDB\Driver\Manager入口类,负责维护与MongoDB的连接,执行读写和命令
MongoDB\Driver\BulkWrite收集要发送到服务器的一个或多个写操作
MongoDB\Driver\Query构造查询对象
MongoDB\Driver\Cursor封装MongoDB命令或查询的结果
MongoDB\Driver\WriteResult封装执行结果

2.实现代码

插入数据

<?php
/**
 * Created by PhpStorm.
 * User: jmsite.cn
 * Date: 2019/1/30
 * Time: 13:12
 */



$manager = new MongoDB\Driver\Manager("mongodb://admin:password@192.168.75.132:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(array(
    'product_id'        => 123,
    'product_name'      => 'Gregory 格里高利登山包B75蓝色新款',
    'product_price'     => 2139.00,
));
$bulk->insert(array(
    'product_id'        => 124,
    'product_name'      => 'Osprey 小鹰登山包Aether AG苍穹 70L蓝色',
    'product_price'     => 2099.00,
));
$bulk->insert(array(
    'product_id'        => 125,
    'product_name'      => 'GRANITE GEAR 花岗岩登山包光环追踪85L红色',
    'product_price'     => 1800.00,
));
$bulk->insert(array(
    'product_id'        => 126,
    'product_name'      => 'ARCTERYX 始祖鸟登山包Bora AR 50L婆罗洲蓝',
    'product_price'     => 4450.00,
));

try {
    $result = $manager->executeBulkWrite('db.product', $bulk);
    var_dump($result->getInsertedCount());
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
    var_dump($e->getWriteResult()->getWriteErrors());
}

执行结果

int(4)

条件查询

$manager = new MongoDB\Driver\Manager("mongodb://admin:password@192.168.75.132:27017");

$filter = ['product_price' => ['$lt' => 4000]];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['product_price' => 1],
];

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('db.product', $query);

foreach ($cursor as $document) {
    print_r($document);
}

执行结果

stdClass Object
(
    [product_id] => 125
    [product_name] => GRANITE GEAR 花岗岩登山包光环追踪85L红色
    [product_price] => 1800
)
stdClass Object
(
    [product_id] => 124
    [product_name] => Osprey 小鹰登山包Aether AG苍穹 70L蓝色
    [product_price] => 2099
)
stdClass Object
(
    [product_id] => 123
    [product_name] => Gregory 格里高利登山包B75蓝色新款
    [product_price] => 2139
)

更新和删除数据

$manager = new MongoDB\Driver\Manager("mongodb://admin:password@192.168.75.132:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->update(array('product_id' => 123), array('$set' => array('product_price' => 1999.99)), array('multi' => false, 'upsert' => false));
$bulk->delete(array('product_id' => 125));

try {
    $result = $manager->executeBulkWrite('db.product', $bulk);
    var_dump($result->getModifiedCount());
    var_dump($result->getDeletedCount());
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
    var_dump($e->getWriteResult()->getWriteErrors());
}

执行结果

int(1)
int(1)

查询更新和删除后的结果

$manager = new MongoDB\Driver\Manager("mongodb://admin:password@192.168.75.132:27017");

$filter = [];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['product_price' => 1],
];

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('db.product', $query);

foreach ($cursor as $document) {
    print_r($document);
}

执行结果

stdClass Object
(
    [product_id] => 123
    [product_name] => Gregory 格里高利登山包B75蓝色新款
    [product_price] => 1999.99
)
stdClass Object
(
    [product_id] => 124
    [product_name] => Osprey 小鹰登山包Aether AG苍穹 70L蓝色
    [product_price] => 2099
)
stdClass Object
(
    [product_id] => 126
    [product_name] => ARCTERYX 始祖鸟登山包Bora AR 50L婆罗洲蓝
    [product_price] => 4450
)