05月07, 2015

ThinkPHP下MongoDB的操作方法

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


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;
    }
}


本文链接:https://lxyit.com/article/show/62.html

-- EOF --