1. 程式人生 > >ThinkPHP下MongoDB的操作方法

ThinkPHP下MongoDB的操作方法

<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function mongo() { $model = D("User"); //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; } }