ThinkPHP

ThinkPHP下MongoDB的操作方法DEMO

作者:本站 2017-07-25 浏览:208 标签: thinkphp    MongoDB    demo    

  https://github.com/liu21st/thinkphp/commits/master/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php?author=vus520  向tp

  https://github.com/liu21st/thinkphp/commits/master/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php?author=vus520

  向tp添加了一些mongodb操作方法,demo如下


<?php
namespace Home\Model;
use Think\Model\MongoModel;
class UserActlogModel extends MongoModel
{
     
}


<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller
{
    function index()
    {
        echo __CLASS__ .'\\'. __FUNCTION__;
    }
 
    function hello()
    {
        echo "Hello world";
    }
 
    public function mongo()
    {
        $model = D("UserActLog");
 
        //distinct查询语法,支持where、cache操作链
        $result = $model->cache(false)->where( array('device.channelid'=>"10017") )->distinct('device.imei', array("day"=>"20140706"));
        print_r(count($result));
        print_r($model->_sql());
        echo PHP_EOL;
 
        //command查询语法
        $data = $model->command(array('buildInfo'=>1));
        //print_r($data);
        print_r($model->_sql());
        echo PHP_EOL;
 
        $data = $model->status();
        //print_r($data);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //MapReduce查询语法
        $key    = array('device.channelid'=>1);
        $init   = array('num'=>0);
        $reduce = "function(obj, prev){prev.num++;}";
        $option = array(
                'table' => 'user_act_log',
                'condition'=>array("day"=>"20140701"),
        );
        $result = $model->cache(true)->where( array("day"=>"20140708") )->group($key, $init, $reduce);
        print_r(count($result));
        print_r($model->_sql());
        echo PHP_EOL;
 
        //find查询语法,支持where、order、cache、field操作链
        //field方法支持field("act,group"),field(array("act"=>true, "_id"=>false)),field(array("act", "group"))
        $result = $model->where(array("day"=>"20140701"))->field(array("act"=>true, "group"=>true, "_id"=>false))->order('_id asc')->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //子查询OR逻辑
        $map['device.channel'] = "obx_03";
        $map['_logic'] = 'and';
        $map['_complex'] = array("act"=>"down", "group"=>"download", '_logic'=>"or");
        $result = $model->field(array("act"=>true, "group"=>true, "_id"=>false))->where($map)->limit(2)->select();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //子查询OR逻辑
        $map['_complex'] = array('_logic'=>"or", array("act"=>"down"), array("group"=>"download"));
        $result = $model->where($map)->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //子查询OR逻辑
        $map = array();
        $map['_complex'] = array('_logic'=>"or", array("act"=>"down"), array("group"=>"download"));
        $result = $model->field(array("act"=>true, "group"=>true, "_id"=>false))->where($map)->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //子查询 AND 逻辑
        $map = array();
        $map['_complex'] = array('_logic'=>"and", array("act"=>"down"), array("group"=>"download"));
        $result = $model->field(array("act"=>true, "group"=>true, "_id"=>true))->where($map)->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //子查询 nor 逻辑
        $map = array();
        $map['_complex'] = array('_logic'=>"nor", array("act"=>"down"), array("group"=>"download"));
        $result = $model->field(array("act"=>true, "group"=>true, "_id"=>true))->where($map)->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //$not查询
        $map = array();
        $map['day'] = '20140710';
        $map['act'] = array('$exists'=>true, '$not'=>array('$in'=>array("click", "listview")) );
        $map['_complex'] = array('_logic'=>"nor", array("act"=>"down"), array("group"=>"download"));
        $result = $model->field(array("act"=>true, "group"=>true, "_id"=>true))->where($map)->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
 
        //$not查询
        $map = array();
        $map['day'] = '20140710';
        $map['_logic'] = 'or';
        $map['act'] = array('$exists'=>true, '$not'=>array('$in'=>array("click", "listview")) );
        $map['_complex'] = array('_logic'=>"nor", array("act"=>"down"), array("group"=>"download"));
        $result = $model->field(array("act"=>true, "group"=>true, "_id"=>true))->where($map)->find();
        print_r($result);
        print_r($model->_sql());
        echo PHP_EOL;
    }
}


  • 0

  • 0

  • 1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.如有文章有版权争议,请给我们留言或者邮件告知我们,邮箱地址:028mw@126.com。