`

lumen操作mongodb

 
阅读更多
前提:使用Eloquent访问mongodb
class ResourceModel extends Eloquent
{
    protected $collection = 'resource';
    protected $connection = 'mongodb';

}


1、新增
class ResourceService extends BaseService
{
	private $collection;
	private $resourceModel;
	public function __construct()
	{
		parent::__construct();
        	$connection =DB::connection('mongodb');
        	$this->collection = $connection->collection('resource');
        	$this->resourceModel = new ResourceModel();
	}

	public function add($array){
		if(!isset($array['rms_uuid']) || !$array['rms_uuid']){
			throw new \Exception('缺少资源ID', 10022);
		}
		$this->collection->insert($array);
        	return $this->collection->where('rms_uuid', $array['rms_uuid'])->get();
    	}
	.......
	.......
}


2、修改
class ResourceService extends BaseService
{	.......
	.......
	public function modify($uuid, $fields)
	{
		$res = $this->getByUuid($uuid);
		if(!$res){
			throw new \Exception('资源不存在', 20027);
		}
		foreach ($fields as $key=>$value) {
			$res->$key = $value;
		}
		$res->modifiedtime = time();
		$res->save();
		return $res;
	}
	.......
	.......
}


3、删除
class ResourceService extends BaseService
{	.......
	.......
	public function remove($uuid)
	{
        	$res = $this->getByUuid($uuid);
        	if(!$res){
			throw new \Exception('资源不存在', 20027);
		}
		$res->delete();
	}
	.......
	.......
}

4、查询
class ResourceService extends BaseService
{	.......
	.......
	public function getByUuid($uuid)
	{
		return ResourceModel::where('rms_uuid', $uuid)->first();
	}
	.......
	.......
}

5、搜索
class ResourceService extends BaseService
{	.......
	.......
        public function search($conditions=[],  $skip = 0, $limit = 200)
	{
		$result = [
			'count' => 0,
			'list' => []
		];

        $tcollection = $this->collection;
        $resourceTypeFieldService = new ResourceTypeFieldService();
        if($conditions && is_array($conditions)) {
            foreach ($conditions as &$condition) {
                if(!isset($condition['field']) || !isset($condition['operator']) || !isset($condition['value'] )){
                    throw new \Exception('无效的查询表达式', 20025);
                }
                if(is_int($condition['field'])){
                    $fieldId = $condition['field'];
                    $field = $resourceTypeFieldService->get($fieldId);
                    if (!$field) {
                        throw new \Exception('无效的查询字段', 20026);
                    }
                    $condition['field'] = $field['field'];
                }
                $field = $condition['field'];
                $operator = $condition['operator'];
                $value = $condition['value'];
                switch($condition['operator']){
                    case '>':
                    case '=':
                    case '<':
                        $tcollection = $tcollection->where($field, $operator, $value);
                        break;
                    case 'exists':
                        $tcollection = $tcollection->where($field, $operator, true);
                        break;
                    case 'all':
                        if(!is_array($value)){
                            throw new \Exception('all操作符只支持数组对象', 20027);
                        }
                        $tcollection = $tcollection->where($field, $operator, $value);
                        break;
                    case 'size':
                        if(!is_int($value)){
                            throw new \Exception('size操作符只支持整数', 20028);
                        }
                        $tcollection = $tcollection->where($field, $operator, $value);
                        break;
                    case 'regex':
                        $value = new Regex($value, '');
                        $tcollection = $tcollection->where($field, $operator, $value);
                        break;
                    case 'type':
                        if(!is_int($value)){
                            throw new \Exception('type操作符只支持整数', 20029);
                        }
                        $tcollection = $tcollection->where($field, $operator, $value);
                        break;
                    case 'mod':
                        if(!is_array($value)){
                            throw new \Exception('mod操作符只支持数组对象', 20031);
                        }
                        $tcollection = $tcollection->where($field, $operator, $value);
                        break;
                    case 'null':
                        $tcollection = $tcollection->whereNull($field);
                        break;
                    case 'in':
                        if(!is_array($value)){
                            throw new \Exception('in操作符只支持数组对象', 20032);
                        }
                        $tcollection = $tcollection->whereIn($field, $value);
                        break;
                    case 'between':
                        if(!is_array($value) || sizeof($value) != 2){
                            throw new \Exception('in操作符只支持数组对象,且数组元素必须2个', 20033);
                        }
                        $tcollection = $tcollection->whereBetween($field, $value);
                        break;
                    default:
                        throw new \Exception('无效的条件操作符:' . $condition['operator'], 20030);
                }

            }
        }
		$count = $tcollection->count();
		if ($count == 0) {
			return $result;
		}
		$list = $tcollection->skip($skip)->take($limit)->get();

		$result['count'] = $count;
		$result['list'] = $list;

		return $result;
	}
	.......
	.......
}

6、聚合


  


  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics