前言
MongoDB 海量数据查询快速 不需要建立数据模型
适合做日志数据库
或者中间数据库 从MySQL数据库中读取存放一些需要读的业务数据
MongoDB没有适合多表事务功能,写操作也没有多好的确定机制,不适合当做业务数据的数据库
MySQl 存放业务数据 事务的读写
实现过程
配置database
<?php
return [
'type' => '\think\mongo\Connection',
'hostname' => 'localhost',
'database' => 'tuzi',
'username' => '',
'password' => '',
'hostport' => '27017',
'dsn' => '',
'params' => [],
'charset' => 'utf8',
'prefix' => '',
];
配置config
'mysql_db' => [
'type' => 'mysql',
'hostname' => 'localhost',
'database' => 'tuzi',
'username' => 'root',
'password' => '111',
'hostport' => '',
'dsn' => '',
'params' => [],
'charset' => 'utf8',
'prefix' => '',
'debug' => true,
'deploy' => 0,
'rw_separate' => false,
'master_num' => 1,
'slave_no' => '',
'fields_strict' => true,
'resultset_type' => 'array',
'auto_timestamp' => false,
'datetime_format' => 'Y-m-d H:i:s',
'sql_explain' => false,
]
代码实现
<?php
namespace app\index\controller;
use think\Db;
class Index
{
public function index()
{
echo "--------我是通过mongodb查询出来的数据----------<br/>";
$data = array('a'=>'元素1','b'=>'元素2');
$res = Db::table('document')->select();
dump($res);
echo "--------我是通过mysql查询出来的数据----------<br/>";
$res1 = Db::connect(Config('mysql_db'))->table('admin')->select();
dump($res1);
return "成功完成";
}
}
运行结果
--------我是通过mongodb查询出来的数据----------
array(3) {
[0] => array(3) {
["_id"] => object(MongoDB\BSON\ObjectId)
["oid"] => string(24) "5a51bf6583869e4b62321af3"
}
["a"] => string(7) "元素1"
["b"] => string(7) "元素2"
}
[1] => array(3) {
["_id"] => object(MongoDB\BSON\ObjectId)
["oid"] => string(24) "5a51bfa083869e4b647a61c6"
}
["a"] => string(7) "元素1"
["b"] => string(7) "元素2"
}
[2] => array(3) {
["_id"] => object(MongoDB\BSON\ObjectId)
["oid"] => string(24) "5a51c30383869e4b674156f6"
}
["a"] => string(7) "元素1"
["b"] => string(7) "元素2"
}
}
--------我是通过mysql查询出来的数据----------
array(1) {
[0] => array(3) {
["admin_id"] => int(1)
["user_name"] => string(5) "admin"
["password"] => string(3) "888"
}
}
成功完成 0.030056s ShowPageTrace