ThinkPHP下MongoDB的操作方法

https://github.com/liu21st/thinkphp/commits/master/ThinkPHP/Library/Think/Db/Driver/Mongo.class.php?author=vus520
向tp添加了一些mongodb操作方法,demo如下

Model

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

Controller

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

发表评论

电子邮件地址不会被公开。