search
HomePHP FrameworkThinkPHPAnalyze the basic operations of mongo under the TP framework and its points of attention

The following thinkphp framework tutorial column will introduce you to the operation of mongo under thinkphp. I hope it will be helpful to friends in need!

Analyze the basic operations of mongo under the TP framework and its points of attention

1. Multiple conditional statements, many on the Internet are just one greater than or one Less than, there are no 2 merged ones, such as

$where['_string'] = 'this.b > 2 & this.b<p>2.group</p><pre class="brush:php;toolbar:false">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 which is greater than 2 and less than 4. If you want to use the model model to query, and your main configuration is mysql , then you need to configure it in the configuration file first

'mongo' => [
DB_TYPE =>  mongo            
DB_HOST => localhost   
DB_NAME => test
DB_PORT =>  40000
DB_PREFIX =>'' 
DB_USER => ''
DB_PWD => ''
],

and then configure it in the model file,
a.protected $trueTableName = 'table name';
b.protected $connection = 'driver name, Here is mongo';
c. This model inherits MongoModel

4. Batch update

mysql里只要$res = $model->save(['a'=>1']);
mongo的话需要写成$res = $model->where([])->save(['a'=>1]);

5. Mongo note:

mongo对于数据类型的控制比较严格,如果你存个int的1,用'1'去查是查不到的!

Recommended: "The latest 10 thinkphp video tutorials"

The above is the detailed content of Analyze the basic operations of mongo under the TP framework and its points of attention. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment