说明
tp3.2.2的Mongodb好像是有bug的,不能直接使用M()方法
- 确认已经安装了Mongodb服务器
- 确认php中已经安装了Mongodb扩展
- TP版本:thinkphp_3.2.3_full.zip
修改Mongodb驱动类
修改ThinkPHP\Library\Think\Db\Driver\Mongo.class.php文件
搜索switchCollection方法,大概第77行添加如下代码
elseif(empty($db) && !$this->_mongo){
// 当前MongoDb对象
$this->_dbName = $this->config['database'];
$this->_mongo = $this->_linkID->selectDb($this->_dbName);
}
switchCollection完整方法
修改完以后其他操作和mysql使用一样
config配置
//Mongodb数据库设置
'DB_TYPE' => 'mongo', // 数据库类型
'DB_PORT' => '27017', // 端口
'DB_PREFIX' => 't_', // 数据库表前缀
'DB_HOST' => '127.0.0.1', // 服务器地址
'DB_NAME' => 'bdata', // 数据库名
'DB_USER' => 'admin', // 用户名
'DB_PWD' => '111111', // 密码
测试
public function index(){
$test['a']='123';
$test['b']='1234';
$test['c']['a']='ca123';
$test['c']['b']='cb123';
M('test')->add($test);
$data=M('test')->select();
dump($data);
exit;
}
使用Mongodb管理工具查看