#1.多個條件語句,網路上很多只是一個大於或一個小於,沒有2個合併的,如大於2且小於4的
$where['_string'] = 'this.b > 2 & this.b<4';
登入後複製
2.group
mysql: $res = $model->where(['sTaskId'=>['$in'=>$task_array]])->group('a')->field('a,sum(a)')->select(); mongodb: $key = ['a'=>1]; //groupby的字段 $init = ['num'=>0];//统计的初始值 $option = array( 'table' => 'course’, // 表名 'condition’=>['sTaskId'=>['$in'=>$task_array]], //group中过滤条件 ); //必须要带option $reduce = "function(obj, prev){prev.num = prev.num+obj.a}"; $model = new TestModel(); $res = $model->group($key, $init, $reduce, $option); 这里讲一下tp的mongo扩展是有问题的,在group里调用where会无效,具体解决方案是要在mongo.class.php文件 a.把$query改为如下: $query = $this->parseWhere(isset($options['condition'])?$options['condition']:array()); 当然$this->queryStr 也要改的 b.把$group改为: $group = $this->_collection->group($keys,$initial,$reduce,$query);
登入後複製
3.如果你要用model模型去查詢,並且你的主設定是mysql ,那就需要先在設定檔設定
'mongo' => [ DB_TYPE => mongo DB_HOST => localhost DB_NAME => test DB_PORT => 40000 DB_PREFIX =>'' DB_USER => '' DB_PWD => '' ],
登入後複製
然後在model檔案裡配置,
a.protected $trueTableName = '表名';
b.protected $connection = '驅動名,這裡是mongo';
c.該model繼承MongoModel
4.批次更新
mysql里只要$res = $model->save(['a'=>1']); mongo的话需要写成$res = $model->where([])->save(['a'=>1]);
登入後複製
5.mongo注意點:
mongo对于数据类型的控制比较严格,如果你存个int的1,用'1'去查是查不到的!
登入後複製