Home >Backend Development >PHP Tutorial >Problems with thinkphp operating mongodb
The mongodb
database connection has been configured in the framework. How to implement the CURD operation? The following is my operation
<code>$model = M('category'); // $model = D('category'); /** 查询 */ $result = $model->where($where)->select();</code>
My idea is that the method that comes with the framework can be operated in the same way as mysql
, but I found that this does not work, and it always says that there is a problem with the instantiation of my M
method. But category does exist in my mongodb. Solve
mongodbdatabase connection has been configured in the framework. How to implement the CURD operation? The following is my operation
<code>$model = M('category'); // $model = D('category'); /** 查询 */ $result = $model->where($where)->select();</code>My idea is that the method that comes with the framework can be operated in the same way as
mysql, but I found that this does not work, and it always says that there is a problem with the instantiation of my
M method. But category does exist in my mongodb
. Solve
M
method. I forgot the specific error and it may be different from yours. I used the D
method and the operation mysql
and there was no problem, but using D
You need to pay attention to the method. Just define the corresponding class in the model file. For example
<pre class="brush:php;toolbar:false"><code><?php
/**
* Description: MongoDB操作
* Author: yangxiangming@live.com
* Date: 2015/9/9
* Time: 13:35
*/
namespace Bbsapi\Model;
use Think\Model\MongoModel;
class ExampleModel extends MongoModel {
}</code></pre>
The calling operation is as follows
<code><?php /** * Description: MongoDB操作 * Author: yangxiangming@live.com * Date: 2015/9/9 * Time: 13:51 */ namespace \Controller; use Think\Model\ExampleModel; class ExampleController extends ExampleModel{ public function example(){ $where['_id'] = '54dd9116e4b061818991ac7d'; $model = D('Example'); /** 查询 */ $result = $model->where($where)->select(); /** 添加 */ $data['name'] = 'Example'; …… $model->add($data); /** 更新 */ $data['name'] = 'ExampleTmp'; …… $model->where($where)->save($data); /** 删除 */ $model->where($where)->delete(); } }</code>
Reference link