前言
请先安装mongodb数据库,并且安装php的mongodb扩展
安装完成后,启动。如果是mac安装,请参考本博主的mongodb类别中的安装篇
步骤
运行mongodb
tp5安装mongodb扩展
使用composer安装
参考地址
https://www.kancloud.cn/manual/thinkphp5/167865
composer require topthink/think-mongo=1.*
安装后vendor的样子
配置
使用
<?php
namespace app\index\controller;
use think\Db;
class Index
{
public function index()
{
$data = array('a'=>'元素1','b'=>'元素2');
// Db::table('document')->insert($data);
$res = Db::table('document')->select();
dump($res);
return "成功完成";
}
}
结果
array(3) {
[0] => array(3) {
["_id"] => object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] => string(24) "5a51bf6583869e4b62321af3"
}
["a"] => string(7) "元素1"
["b"] => string(7) "元素2"
}
[1] => array(3) {
["_id"] => object(MongoDB\BSON\ObjectId)#13 (1) {
["oid"] => string(24) "5a51bfa083869e4b647a61c6"
}
["a"] => string(7) "元素1"
["b"] => string(7) "元素2"
}
[2] => array(3) {
["_id"] => object(MongoDB\BSON\ObjectId)#14 (1) {
["oid"] => string(24) "5a51c30383869e4b674156f6"
}
["a"] => string(7) "元素1"
["b"] => string(7) "元素2"
}
}
成功完成 0.020786s ShowPageTrace
参考地址
https://www.kancloud.cn/danielxss/v20170712/354651
https://www.kancloud.cn/manual/thinkphp5/167865