• php7Mongodb操作CURD和聚合分组求和


    <?php
    //初始化
    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    
    // 插入数据
    $bulk = new MongoDBDriverBulkWrite;
    $bulk->insert(['x' => 1, 'name'=>'菜鸟教程', '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']);
    $manager->executeBulkWrite('test.sites', $bulk);
    
    /**
     *  $lt  小于
     *  $lte 小于等于
     *  $gt  大于
     *  $gte 大于等于
     *  $ne  不等于
     *  $in  包含
     *  $nin 不包含
     * */
    $filter = ['x' => ['$gt' => 0]]; //大于
    $filter = ['x' => ['$lt' => 5]]; //小于
    $filter = ['name' => 'Google'];  //等于
    $filter = ['name' => ['$regex' => '教程']]; //模糊查询
    $filter = ['x' => ['$in' => [1,2]]]; //in用法
    $options = [
        'projection' => ['_id' => 0], //key为查询的字段 value 1:显示 0:隐藏
        'sort' => ['x' => 1], //1升序 -1降序
        'limit' => 0, //限制查询条数
    ];
    
    // 查询数据
    $query = new MongoDBDriverQuery($filter, $options);//查询请求
    $cursor = $manager->executeQuery('test.sites', $query);
    foreach ($cursor as $document) {
        $document = objectToArray($document);
        print_r($document);
    }
    
    //更新
    $bulk = new MongoDBDriverBulkWrite;
    $bulk->update(
        ['x' => 2],
        ['$set' => ['name' => '菜鸟工具', 'url' => 'tool.runoob.com']],
        ['multi' => false, 'upsert' => false]
    );
    $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
    $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
    var_dump($result);
    
    //删除
    $bulk = new MongoDBDriverBulkWrite;
    $bulk->delete(['x' => 1], ['limit' => 1]);   // limit 为 1 时,删除第一条匹配数据
    $bulk->delete(['x' => 2], ['limit' => 0]);   // limit 为 0 时,删除所有匹配数据
    $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
    $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
    var_dump($result);
    
    //聚合查询 $match:分组搜索条件 count:分组后求个数 sum:分组后求和
    $param = [
        'aggregate' => 'sites', //表名
        'pipeline'  => [
            ['$match' => ['x' => 2]],
            ['$group' => ['_id' => '$name', 'count' => ['$sum' => 1]]],
        ],
        'cursor'    => new stdClass()// 高版本需要带上这个值
    ];
    
    $command = new MongoDBDriverCommand($param);
    $cursor = $manager->executeCommand('test', $command);
    foreach ($cursor as $document) {
        $document = objectToArray($document);
        var_dump($document);
    }
    
    function objectToArray($object){
        $result = array();
        $object = is_object($object) ? get_object_vars($object) : $object;
        foreach ($object as $key => $val) {
            $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val;
            $result[$key] = $val;
        }
        return $result;
    }
  • 相关阅读:
    详细分析Orchard的Content、Drivers, Shapes and Placement 类型
    什么是现代的应用程序?
    MySQL锁详解!(转载)
    EF DataFirst修改数据类型
    EF+LINQ事物处理
    .net防止SQL注入的一种方式
    .net解决Xss攻击
    IDEA创建Struts2报错——web.xml
    WebServer搭建过程
    设计模式——观察者模式
  • 原文地址:https://www.cnblogs.com/dawuge/p/14583986.html
Copyright © 2020-2023  润新知