First, add a database connection with MongoDB in config.php
‘DB_TYPE‘=>‘mongo‘,// 数据库类型
‘DB_HOST‘=>‘localhost‘,// 服务器地址
‘DB_NAME‘=>‘fzdb‘,// 数据库名
‘DB_USER‘=>‘‘,// 用户名
‘DB_PWD‘=>‘‘,// 密码
‘DB_PORT‘=>27017,// 端口
‘DB_PREFIX‘=>‘fz_‘,// 数据库表前缀
‘DB_CHARSET‘=>‘utf8‘,// 数据库字符集
indexcontroller.class.php Create a MongoDB module inside the controller
\Think\Build::buildModel(‘Home‘, ‘Student‘);
Then go to the model and modify the file.
<?php
namespace Home\Model;
use Think\Model\MongoModel;
class StudentModel extends MongoModel {
}
Querying data
MongoDB Database Add information
$m=D(‘student‘);
//添加数据信息
$d[‘sname‘]=‘张三丰‘;
$m->add($d);
MongoDB database query Information
$m=D(‘student‘);
$rs=$m->select();
dump($rs);
$rs = $m->field (' sname ')->select ();
Field fields name modify data information by ID with Save
//修改数据信息
$d[‘_id‘]=‘559c983564f7676816000029‘;
$d[‘sname‘]=‘连少蕊‘;
$m->save($d);
Deletes the specified information by ID
//删除指定的id
$m->delete(‘559c984264f767d00b000029‘);
From for notes (Wiz)
Thinkphp with MongoDB