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'去查是查不到的!